如何对数据框的列进行分组以在 pandas 中列出? [英] How to group the the columns of dataframe to list in pandas?
本文介绍了如何对数据框的列进行分组以在 pandas 中列出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
member_srl click_day productid
0 6963 20170106 3927352
1 6963 20170106 3790726
2 6963 20170106 977962
3 6963 20170106 1393860
4 6963 20170106 3759353
这是我的df,我想将member_srl和click_day分组,以获取productid列表.例如,member_srl 6963和click_day 20170106将与产品列表相对应:[3927352,3790726,977962,1393860,3759353]
Here is my df and I want to group the member_srl and click_day, to get the list of productid. For example, member_srl 6963 and click_day 20170106 will correspond to the product list: [3927352,3790726,977962,1393860,3759353]
谢谢.
推荐答案
df = df.groupby(['member_srl','click_day'])['productid'].apply(list)
print (df)
member_srl click_day
6963 20170106 [3927352, 3790726, 977962, 1393860, 3759353]
Name: productid, dtype: object
df = df.groupby(['member_srl','click_day'])['productid'].apply(list).reset_index()
print (df)
member_srl click_day productid
0 6963 20170106 [3927352, 3790726, 977962, 1393860, 3759353]
这篇关于如何对数据框的列进行分组以在 pandas 中列出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文