I have a pandas DataFrame as shown below.
df = pd.DataFrame({
\'date\': [\'2011-01-01\', \'2011-01-01\', \'2011-02-01\', \'2011-02-01\', \'2011-03-01\',
Use groupby
:
df.groupby('category').ffill()
Output:
category date rate
0 1 2011-01-01 0.50
1 2 2011-01-01 0.75
2 1 2011-02-01 0.50
3 2 2011-02-01 0.75
4 1 2011-03-01 1.00
5 2 2011-03-01 1.25
6 1 2011-04-01 1.00
7 2 2011-04-01 1.25
If you have other columns with NaN that you don't want fill, then you can use this to just ffill NaN in rate column:
df['rate'] = df.groupby('category')['rate'].ffill()