Reputation: 1393
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
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