remove duplicates while adding a column in csv file using python

127 Views Asked by At

I have a CSV file that looks like this:

|innings |     bowler    |
|--------|---------------|                      
|1       |      P Kumar  |
|1       |      P Kumar  |
|1       |      P Kumar  |
|1       |      P Kumar  |
|1       |      Z Khan   |
|1       |      Z Khan   |
|1       |      Z Khan   |
|2       |      AB Dinda |
|2       |      AB Dinda |
|2       |      I Sharma |

Desired Output

|innings |     bowler           |
|--------|----------------------|
|1       |    P Kumar,Z Khan    |
|2       |    AB Dinda,I Sharma |

Code I Applied:

df.groupby(['innings']).bowler.sum().drop_duplicates(subset="bowler",keep='first',inplace=True)

but for some reason, it is giving me an error TypeError: drop_duplicates() got an unexpected keyword argument 'subset'

then i tried without subset: drop_duplicates("bowler",keep='first', inplace=True) now i am getting this error TypeError: drop_duplicates() got multiple values for argument 'keep'

1

There are 1 best solutions below

2
On

Use DataFrame.drop_duplicates first by both columns and then aggregate join:

df = (df.drop_duplicates(subset=["bowler",'innings'])
        .groupby('innings')
        .bowler.agg(','.join)
        .reset_index())

print (df)
   innings             bowler
0        1     P Kumar,Z Khan
1        2  AB Dinda,I Sharma