加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 在pandas数据帧中透视表

发布时间:2020-12-20 13:03:30 所属栏目:Python 来源:网络整理
导读:我有一个要求,我在尝试计算值并将它们放在数据透视表中. 这是我的数据帧, Cola Colb Apple Rippened Orange Rippened Apple UnRippened Mango UnRippened 我希望输出像这样, Rippened UnRippenedApple 1 1Mango 0 1Orange 1 0 请分享您的想法. 解决方法 IIUC
我有一个要求,我在尝试计算值并将它们放在数据透视表中.

这是我的数据帧,

Cola        Colb          
 Apple    Rippened 
Orange    Rippened
 Apple  UnRippened
 Mango  UnRippened

我希望输出像这样,

Rippened  UnRippened
Apple          1           1
Mango          0           1
Orange         1           0

请分享您的想法.

解决方法

IIUC:

In [178]: d.pivot_table(index='Cola',columns='Colb',aggfunc='size',fill_value=0)
Out[178]:
Colb    Rippened  UnRippened
Cola
Apple          1           1
Mango          0           1
Orange         1           0

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读