[英]Pandas - Group by one column and aggregate other column to list
I have a dataframe that has multiple entries for users.我有一个 dataframe 有多个用户条目。 These users can also be assigned to multiple ID's.
这些用户也可以分配到多个 ID。
I would like to group by the users and then store a list of these ID's in another column as shown below:我想按用户分组,然后将这些 ID 的列表存储在另一列中,如下所示:
I'd like to go from this:我想从这个 go :
df1 = pd.DataFrame({'USER': ['BOB','STEVE','PAUL','KEITH','STEVE','STEVE','BOB'],'ID':[1,2,3,4,5,6,7]})
To this.对此。 Only showing values if that user is attached to multiple ID's
仅当该用户附加到多个 ID 时才显示值
groupby
+ map
groupby
+ map
u = df1.groupby("USER")["ID"].agg(list)
df1["MULTI_IDS"] = df1["USER"].map(u[u.str.len().ge(2)])
USER ID MULTI_IDS
0 BOB 1 [1, 7]
1 STEVE 2 [2, 5, 6]
2 PAUL 3 NaN
3 KEITH 4 NaN
4 STEVE 5 [2, 5, 6]
5 STEVE 6 [2, 5, 6]
6 BOB 7 [1, 7]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.