Raul Benitez
Raul Benitez

Reputation: 35

Duplicated rows merge in one row adding new columns in pandas

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

Answers (2)

Pyd
Pyd

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

BENY
BENY

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

Related Questions