Kvothe
Kvothe

Reputation: 1393

Pandas duplicated rows to unique rows

I have a dataframe that looks like this:

df = pd.DataFrame({'Id':['1', '1', '1', '2', '2'], 'Name':['A', 'B', 'C', 'A', 'B']})

What I want is to get something like this.

df1 = pd.DataFrame({'Id':['1', '2'], 'Name':['A', 'B', 'C', 'A', 'B']})

I had a dataframe like above but I had to do some analysis so I split it using the following code:

df2 = pd.concat([Series(row['Id'], row['Name'].split(',')) for _, row in df.iterrows()]).reset_index()

I'm looking to reverse the code, but I'm stuck.

Any help is much appreciated!

Upvotes: 1

Views: 82

Answers (1)

Evan Wright
Evan Wright

Reputation: 690

Try this:

df.groupby('Id')['Name'].apply(','.join)

You may need to reset the index and rename a column, but this should do the main piece of the job.

Upvotes: 1

Related Questions