Eisen
Eisen

Reputation: 1887

Merge rows with same identifier columsn (PANDAS)

i want to combine rows that have the same id and variant. this is my expected input and output how can i do this in pandas? input:

id company tag              variant
1  Adobe   ['hello','bye']  B
1  Adobe   ['gary,'tom']    B
1  Adobe   ['tim','john']   A
1  Adobe   ['fries,'salad'] A

output:

id company tag                                variant
1  Adobe   ['hello','bye'],[['gary,'tom']]    B
1  Adobe   [['tim','john'],['fries,'salad']]  A

Upvotes: 1

Views: 41

Answers (1)

piRSquared
piRSquared

Reputation: 294218

agg

df.groupby(['id', 'variant'], as_index=False).agg({'company': 'first', 'tag': list})

   id variant company                            tag
0   1       A   Adobe  [[tim, john], [fries, salad]]
1   1       B   Adobe    [[hello, bye], [gary, tom]]

Upvotes: 2

Related Questions