gpt4 book ai didi

python-3.x - 将项目数输出为标题 Pandas

转载 作者:行者123 更新时间:2023-12-02 06:46:22 27 4
gpt4 key购买 nike

这是我的数据框:'''

 customer product     product1
0 A hats shoes
1 A socks shoes
2 B socks shoes
3 C hats shoes
4 C None accessories
5 B socks shoes
6 A hats shoes
7 C None accessories

'''我想输出这样的东西:

customer    shoes   hats    socks   accessories
A # # # #
B # # # #
C # # # #

我试过这样分组:'''

dfB.set_index('customer').groupby(['product', 'product1']).agg({'product':['count'], 'product1':['count']}) '''

我得到这样的输出:

'''

 product product1
count count
product product1
hats shoes 3 3
socks shoes 3 3

'''

请帮忙

最佳答案

国际联合会

我们可以将索引设置为 'customer' 然后堆叠数据框,允许您使用 value_counts 进行聚合

df2 = df.set_index('customer').stack().groupby(level=0).value_counts().unstack()

-

print(df2)
None accessories hats shoes socks
customer
A NaN NaN 2.0 3.0 1.0
B NaN NaN NaN 2.0 2.0
C 2.0 2.0 1.0 1.0 NaN

如果你不关心None,你可以将它转换成一个真正的空值,它会在groupby中被忽略

print(df.replace('None',np.nan).set_index('customer').stack().groupby(level=0).value_counts().unstack())

accessories hats shoes socks
customer
A NaN 2.0 3.0 1.0
B NaN NaN 2.0 2.0
C 2.0 1.0 1.0 NaN

关于python-3.x - 将项目数输出为标题 Pandas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60099669/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com