Pandas - Group by one column and aggregate other column to list
I have a dataframe that has multiple entries for users. These users can also be assigned to multiple ID's.
I would like to group by the users and then store a list of these ID's in another column as shown below:
I'd like to go from this:
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
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]