Groupby在Pandas中运行以组合字符串值

问题描述 投票:0回答:2

我的df采用以下格式:

enter image description here

当我做一个df.groupby('工具')。sum()我得到以下内容:

enter image description here

预期的输出如下:

enter image description here

请指导我实现所需的输出。

python pandas group-by pivot-table
2个回答
2
投票

使用GroupBy.agg将值转换为sets,然后通过/加入:

df.groupby('Tool').agg(lambda x: '/'.join(set(x))) 

0
投票
#You can also use-
df.groupby('Tool').agg(min("Cost Center") as "Cost Center",min("Nature") as "Nature").show
© www.soinside.com 2019 - 2024. All rights reserved.