Reputation: 35
I have this data
| id | name | action|
| 1 | user1| action1|
| 2 | user2| action1|
| 3 | user1| action56|
| 4 | user3| action49|
| 5 | user1| action649|
and I want to get this:
| id | name | action | action2 | action3 |
| 1 | user1| action1| action56| action649|
| 2 | user2| action1|
| 4 | user3| action49|
I already know how to get the firsts duplicates and last ones, but I don't know how to "transform" the dataframe
Upvotes: 0
Views: 43
Reputation: 6159
you need groupby.transform,
df['action']=df.groupby('name')['action'].transform(','.join)
df=df.drop_duplicates('name')
print(df)
#id name action
#0 1 user1 action1, action56, action649
#1 2 user2 action1
#3 4 user3 action49
Upvotes: 0
Reputation: 323226
This is a pivot
problem after using groupby
+ cumcount
create the key
df.assign(key=df.groupby('name').cumcount()+1).pivot('name','key','action').add_prefix('action_')
key action_1 action_2 action_3
name
user1 action1 action56 action649
user2 action1 None None
user3 action49 None None
Upvotes: 2