Reputation: 382
I have one Dataframe.
Dataframe1:
desc id result
A 1 Yes
A 2 No
A 3 Yes
A 4 No
B 1 No
B 2 Yes
C 1 Yes
Output:
Dataframe2:
id result
1 A_Yes, B_No, C_Yes
2 A_No, B_Yes
3 A_Yes
4 A_No
5
It is simply concat desc and result and then group by.
How should i do it?
Upvotes: 0
Views: 55
Reputation: 323226
We using pandas.Series.groupby
(df.desc+'_'+df.result).groupby(df['id']).apply(','.join).reset_index(name='result')
Out[207]:
id result
0 1 A_Yes,B_No,C_Yes
1 2 A_No,B_Yes
2 3 A_Yes
3 4 A_No
Upvotes: 3
Reputation: 42886
You can concat the desc
and result
column and after that use pandas.groupby.DataFrame.agg
df['result'] = df['desc'] + '_' + df['result']
df = df.groupby('id').agg({'result': ', '.join})
print(df)
result
id
1 A_Yes, B_No, C_Yes
2 A_No, B_Yes
3 A_Yes
4 A_No
Upvotes: 1