我有一个数据框,其中有 4 列 User_id、Transaction_id、产品和日期时间。对于每个用户,我必须选择他最近的前 n 笔交易,假设 n=2,我的数据框如下:
transaction_id user_id product date
T1 U1 P1 2019-03-27
T1 U1 P2 2019-03-27
T1 U1 P3 2019-03-27
T2 U1 P2 2019-03-21
T2 U1 P3 2019-03-21
T3 U1 P2 2019-03-20
我尝试通过此 group by pandas dataframe and select latest in each group 的帮助来做到这一点
我期望的输出是:
transaction_id user_id product date
T1 U1 P1 2019-03-27
T1 U1 P2 2019-03-27
T1 U1 P3 2019-03-27
T2 U1 P2 2019-03-21
T2 U1 P3 2019-03-21
最佳答案
想法是首先通过 DataFrame.drop_duplicates
删除重复项,获取每组的 top2 值和 DataFrame.merge
原始数据框:
df = (df.merge(df.drop_duplicates(['user_id','date'])
.sort_values('date',ascending = False)
.groupby('user_id')
.head(2)[['user_id','date']])
)
print (df)
transaction_id user_id product date
0 T1 U1 P1 2019-03-27
1 T1 U1 P2 2019-03-27
2 T1 U1 P3 2019-03-27
3 T2 U1 P2 2019-03-21
4 T2 U1 P3 2019-03-21
关于python - 根据groupby条件过滤前n行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55374127/