保持到每组值最后一次出现

这是我的熊猫数据框的简化示例:


     User  Binary

0   UserA       0

1   UserA       0

2   UserA       0

3   UserA       1

4   UserA       0

5   UserA       1

6   UserA       0

7   UserA       0

8   UserB       0

9   UserB       0

10  UserB       0

11  UserB       0

12  UserB       0

13  UserB       1

14  UserB       1

15  UserB       0

16  UserC       0

17  UserC       0

对于每个用户,我想在第一次出现 Binary=1 之后删除所有行。注意,会有一些用户没有 Binary=1 的情况,例如本例中的 UserC。


输出如下所示:


     User  Binary

0   UserA       0

1   UserA       0

2   UserA       0

3   UserA       1

8   UserB       0

9   UserB       0

10  UserB       0

11  UserB       0

12  UserB       0

13  UserB       1

16  UserC       0

17  UserC       0


青春有我
浏览 76回答 2
2回答

胡说叔叔

这是使用groupby自定义函数和进行转换的一种方法:# check which Binary values are 1 and group the series by Userg = df.Binary.eq(1).groupby(df.User)# transform to either idxmax or the last index depending# on whether there are any Trues or notm = g.transform(lambda x: x.idxmax() if x.any() else x.index[-1])# index the dataframe where the index is smaler or eq mout = df[df.index <= m]print(out)&nbsp; &nbsp; &nbsp;User&nbsp; Binary0&nbsp; &nbsp;UserA&nbsp; &nbsp; &nbsp; &nbsp;01&nbsp; &nbsp;UserA&nbsp; &nbsp; &nbsp; &nbsp;02&nbsp; &nbsp;UserA&nbsp; &nbsp; &nbsp; &nbsp;03&nbsp; &nbsp;UserA&nbsp; &nbsp; &nbsp; &nbsp;18&nbsp; &nbsp;UserB&nbsp; &nbsp; &nbsp; &nbsp;09&nbsp; &nbsp;UserB&nbsp; &nbsp; &nbsp; &nbsp;010&nbsp; UserB&nbsp; &nbsp; &nbsp; &nbsp;011&nbsp; UserB&nbsp; &nbsp; &nbsp; &nbsp;012&nbsp; UserB&nbsp; &nbsp; &nbsp; &nbsp;013&nbsp; UserB&nbsp; &nbsp; &nbsp; &nbsp;116&nbsp; UserC&nbsp; &nbsp; &nbsp; &nbsp;017&nbsp; UserC&nbsp; &nbsp; &nbsp; &nbsp;0

SMILET

想法是按交换顺序测试连续值的最大值DataFrame.iloc,如果仅0或仅1正确分组值,什么也有效:def f(x):&nbsp; &nbsp; s = x.cumsum()&nbsp; &nbsp; return s.eq(s.max())df = df[df.iloc[::-1].groupby('User')['Binary'].transform(f).sort_index()]print (df)&nbsp; &nbsp; &nbsp;User&nbsp; Binary0&nbsp; &nbsp;UserA&nbsp; &nbsp; &nbsp; &nbsp;01&nbsp; &nbsp;UserA&nbsp; &nbsp; &nbsp; &nbsp;02&nbsp; &nbsp;UserA&nbsp; &nbsp; &nbsp; &nbsp;03&nbsp; &nbsp;UserA&nbsp; &nbsp; &nbsp; &nbsp;18&nbsp; &nbsp;UserB&nbsp; &nbsp; &nbsp; &nbsp;09&nbsp; &nbsp;UserB&nbsp; &nbsp; &nbsp; &nbsp;010&nbsp; UserB&nbsp; &nbsp; &nbsp; &nbsp;011&nbsp; UserB&nbsp; &nbsp; &nbsp; &nbsp;012&nbsp; UserB&nbsp; &nbsp; &nbsp; &nbsp;013&nbsp; UserB&nbsp; &nbsp; &nbsp; &nbsp;116&nbsp; UserC&nbsp; &nbsp; &nbsp; &nbsp;017&nbsp; UserC&nbsp; &nbsp; &nbsp; &nbsp;0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python