How do I prevent the .ffill()
method to drop my grouping variable without overwriting my val
column?
That is, not doing: samp_df['val'] = samp_df.groupby('ID').ffill()
Further, why does .groupby
behaves this way and not the same way it does with .mean()
, moving my grouping variable to the index?
import pandas as pd
import numpy as np
samp_df = pd.DataFrame({'ID': ['A', 'A', 'A', 'B', 'B', 'B'],
'val': [1, 2, np.nan, np.nan, 4, np.nan]})
print(samp_df.groupby('ID').mean())
val
ID
A 1.5
B 3.5
print(samp_df.groupby('ID').ffill())
val
0 1.0
1 2.0
2 2.0
3 NaN
4 4.0
5 4.0