简体   繁体   中英

How can I merge,Join row cells that have same values in pandas data frame

I have the following data frame;

在此处输入图片说明

I would like to merge the cells that contain the same values in the concurrent rows and also join the first field to one cell. An output that I am after based on this is

在此处输入图片说明

I have tried a few Groupby options but with no luck

You can groupby multiple columns, then aggregate the matching Field 1 into a list:

df = pd.DataFrame({'Field 1':['A', 'B', 'A', 'B'],'Field 2':[1, 1, 2, 1], 'Field 3': [2, 2, 3, 12], 'Field 4': [3, 3, 4, 13]})

dfg = df.groupby(['Field 2', 'Field 3', 'Field 4'], as_index=False).agg({
    'Field 1': lambda x : ', '.join(sorted(list(x)))  })

to get dfg:

   Field 2  Field 3  Field 4 Field 1
0        1        2        3    A, B
1        1       12       13       B
2        2        3        4       A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM