I have a large data set in the following format:
id, socialmedia
1, facebook
2, facebook
3, google
4, google
5, google
6, twitter
7, google
8, twitter
9, snapchat
10, twitter
11, facebook
I want to group by then and assign a group_id column and then ungroup (expand) back to individual records.
id, socialmedia, groupId
1, facebook, 1
2, facebook, 1
3, google, 2
4, google, 2
5, google, 2
6, twitter, 3
7, google, 2
8, twitter, 3
9, snapchat, 4
10, twitter, 3
11, facebook, 1
I tried following but end up with 'DataFrameGroupBy' object does not support item assignment.
x['grpId'] = x.groupby('socialmedia')['socialmedia'].rank(method='dense').astype(int)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…