Reputation: 111
This is the input dataframe that I'm having.
And this is the output that I want:
As you can see, both the dataframes are merged on the column Key1 in such a way that the row with common elements are separated by commas.
I have tried using merge but it doesn't give the correct output.
mer = pd.merge(df,df, on='Key1', how='inner')
Is there a specific way to approach this?
Upvotes: 2
Views: 49
Reputation: 863166
You can convert values to strings and join unique values by ,
in custom lambda function:
Solution for working with missing values in Key1
in oldier pandas versions with replace by temp values:
df1 = (df.fillna({'Key1': 'missing'})
.groupby('Key1')
.agg(lambda x: ','.join(pd.unique(x.astype(str))))
.reset_index()
.replace({'Key1':{'missing':np.nan}}))
Solution for last pandas versions:
df1 = (df.groupby('Key1')
.agg(lambda x: ','.join(pd.unique(x.astype(str))))
.reset_index())
Upvotes: 2