I have a dataframe as shown below
Using pandas dataframe I want to replace empty values in a column from first row in a groupby condition based on previous month last value
till last date exists based on each ID Sector Usage, price column value should be filled.
ID Sector Usage Price Date
1 A R 20 29/08/2022
1 A R 30 30/08/2022
1 A R 40 31/08/2022
1 A R 01/09/2022
1 A R 02/09/2022
. . . . .
. . . . .
1 A R 30/09/2022
. . . . .
. . . . .
1 A R 31/10/2022
. . . . .
. . . . .
1 A R 30/11/2022
2 B C 200 31/08/2022
3 B R 60 31/08/2022
expected_output
ID Sector Usage Price Date
1 A R 20 29/08/2022
1 A R 30 30/08/2022
1 A R 40 31/08/2022
1 A R 01/09/2022
1 A R 02/09/2022
. . . . .
. . . . .
1 A R 40 30/09/2022
. . . . .
. . . . .
1 A R 40 31/10/2022
. . . . .
. . . . .
1 A R 40 30/11/2022
2 B C 200 31/08/2022
2 B C 200 01/09/2022
. . . . .
. . . . .
2 B C 200 31/10/2022
. . . . .
. . . . .
2 B C 200 31/12/2022
3 B R 60 31/08/2022
I have tried below codes but not working
m = df['Price'] == ''
s = df.assign(Price=df['Price'].mask(m)).groupby(['Sector','Usage'])['Price'].ffill()
df['Price'] = np.where(m, s, df['Price']).astype(int)
or
df.replace({'Price': {0:np.NaN}}).ffill()