在Pandas数据框中旋转数据表 [英] Pivoting tables in a pandas dataframe

查看:74
本文介绍了在Pandas数据框中旋转数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我要在其中计算值并将它们放在数据透视表中.

I have a requirement where in I am trying to count the values and Put them in the pivot table.

这是我的数据框,

  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

这篇关于在Pandas数据框中旋转数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆