pandas dataframe中的行连接-新版本

zdwk9cvp  于 2023-04-28  发布在  其他
关注(0)|答案(1)|浏览(146)

我有下一张table。

import pandas as pd
# Define the input data
data = {
    'ID': [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3],
    'count': [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,1,1,1,1,2,2,1,1,1,1,2],
    'priority': [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,4,3,1,2,3,4,4],
    'item': ['A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','D','C','A','B','C','D','D'],
    'c': ['XX','XX','XX','XX','YY-SS','YY','YY','YY','YY-SS','YY','YY','YY','XX','XX','XX','XX','ZZ','ZZ','ZZ','ZZ','ZZ','ZZ','TT-SS','ZZ','ZZ','ZZ','ZZ']
}

# Convert the input data to a Pandas DataFrame
df = pd.DataFrame(data)

我需要转换这个输入,你可以在下面的输出示例中看到:在此输入图像描述

如果你有任何想法请分享。非常感谢!

mrwjdhj3

mrwjdhj31#

您可以使用自定义groupby.agg

out = (df
   .sort_values(by='priority') # optional
   .groupby(['ID', 'count'], as_index=False)
   .agg({'item': '-'.join, 'c': 'first'})
   .assign(FINAL=lambda d: d.pop('item')+'-'+d.pop('c'))
   .drop(columns='count')
)

输出:

ID          FINAL
0   1     A-B-C-D-XX
1   1  A-B-C-D-YY-SS
2   1  A-B-C-D-YY-SS
3   1     A-B-C-D-XX
4   2     A-B-C-D-ZZ
5   2         D-C-ZZ
6   3  A-B-C-D-TT-SS
7   3           D-ZZ

相关问题