I have dataframe and i would like to groupby but get the countries in one cell
Brand Model Country
IPhone 6 UK
IPhone 7 DE
IPhone 6 US
IPhone 7 FR
IPhone 6 IT
IPhone 7 NL
I would like to have this result would this be posssible
Brand Model Country
IPhone 6 UK - US - IT
IPhone 7 DE - FR - NL
CodePudding user response:
This should work:
df = df.groupby(['Brand', 'Model']).agg({'Country': lambda x: '-'.join(x)}).reset_index()
result:
Model Brand Country
0 6 iphone UK-US-IT
1 7 iphone DE-FR-NL
CodePudding user response:
Try this :
out = (df.groupby(['Brand', 'Model'])
.agg({'Country':'unique'})
.applymap(lambda x: ' - '.join([str(val) for val in list(x)]))
.reset_index()
)