I have a table that looks like this
index | Group | rank | Values |
---|---|---|---|
0 | a | 2 | 344.0 |
1 | a | 3 | NaN |
2 | b | 1 | 455.0 |
3 | a | 1 | NaN |
4 | b | 2 | NaN |
I want to group data by 'Group', then sort according to 'rank' and then bfill only for rank == 1. The dataset is very big so I want to avoid loops.
I tried
temp[temp['rank']<=2].sort_values('rank', ascending = True).groupby('Group').bfill(axis='rows', inplace = True)
but this gives me "backfill() got an unexpected keyword argument 'axis'"
CodePudding user response:
Remove non exist parameters axis='rows', inplace = True
in GroupBy.bfill
and assign back to filtered rows:
m = temp['rank']<=2
temp.loc[m, 'Values'] = temp[m].sort_values('rank').groupby('Group')['Values'].bfill()
CodePudding user response:
df.sort_values(by = 'rank', inplace = True)
df = df.assign(Values1 = lambda x: x['rank'] == 1).fillna(method = 'bfill')
df.groupby(by = 'Group')['Values']