Combine rows by id
Asked Answered
H

1

1

I have the following types of data. How to combine rows by id, and the type record a separate column (all types 10) (or record types, separated by commas in a single column)

id,type,value
1,8,value1
1,2,value1
1,7,value1
1,3,value1
1,10,value1
2,3,value1
2,8,value1
2,7,value1

desired output:

 id        type    value 
0   1  8,2,7,3,10  value1 
1   2       3,8,7  value1
Hindgut answered 24/11, 2016 at 17:36 Comment(1)
Have you tried something yet? Can you post the code you are using?Galop
D
4

I think you can use groupby with apply join, but first convert int to str:

df = df.groupby('id')['type'].apply(lambda x: ','.join(x.astype(str))).reset_index()
print (df)
   id        type
0   1  8,2,7,3,10
1   2       3,8,7
Demodulate answered 24/11, 2016 at 17:39 Comment(10)
as a group when there are three columns (the third has the same value) - one group, to collect in the second list and a third leave? @DemodulateHindgut
I am not sure if understand. Can you post new question with sample and desired output?Demodulate
value1 is same?Demodulate
If yes, add column to groupby like df = df.groupby(['id', 'value'])['type'].apply(lambda x: ','.join(x.astype(str))).reset_index()Demodulate
what to do if there are different values? @DemodulateHindgut
Then need aggregate by agg - e.g. df.groupby('id').agg({'type': lambda x: ','.join(x.astype(str)), 'value': 'mean'}).reset_index()Demodulate
'mean' - Only for numeric values @DemodulateHindgut
There are strings? Then it is possible use df.groupby('id').agg({'type': lambda x: ','.join(x.astype(str)), 'value': lambda x: ','.join(x)}).reset_index() or df.groupby('id').agg({'type': lambda x: ','.join(x.astype(str)), 'value': 'first'}).reset_index()Demodulate
the second option is suitable, thank you very much @DemodulateHindgut
Super. Btw, in future better is create new question as edited old, becasue now my old answer not matched with new edited question ;)Demodulate

© 2022 - 2024 — McMap. All rights reserved.