1 回答
TA贡献1783条经验 获得超4个赞
done通过 back by 的累积和比较值和 cretae 组iloc[::-1],最后添加另一个iloc[::-1]列的原始顺序:
g = df['status'].eq('done').iloc[::-1].cumsum().iloc[::-1]
grouper = df.groupby(g, sort=False)
样本:
#chnaged data for more groups
print (df)
id price status
0 1 23 none
1 2 23 done
2 3 34 none
3 4 32 none
4 5 31 done
5 6 37 none
6 7 20 none
7 8 29 none
8 9 21 none
9 10 22 done
g = df['status'].eq('done').iloc[::-1].cumsum().iloc[::-1]
print (g)
0 3
1 3
2 2
3 2
4 2
5 1
6 1
7 1
8 1
9 1
Name: status, dtype: int32
grouper = df.groupby(g, sort=False)
for name, df in grouper:
print (df)
id price status
0 1 23 none
1 2 23 done
id price status
2 3 34 none
3 4 32 none
4 5 31 done
id price status
5 6 37 none
6 7 20 none
7 8 29 none
8 9 21 none
9 10 22 done
添加回答
举报