使用枢轴的 Pandas KeyError [英] Pandas KeyError using pivot

查看:101
本文介绍了使用枢轴的 Pandas KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,我想使用 Python 来复制常见的 excel 任务.如果已经回答了这样的问题,请告诉我.我一直无法找到它.我有以下熊猫数据框(数据):

I'm new to Python and I would like to use Python to replicate a common excel task. If such a question has already been answered, please let me know. I've been unable to find it. I have the following pandas dataframe (data):

Date    Stage   SubStage    Value
12/31/2015   1.00   a   0.896882891
1/1/2016     1.00   a   0.0458843
1/2/2016     1.00   a   0.126805588
1/3/2016     1.00   b   0.615824461
1/4/2016     1.00   b   0.245092069
1/5/2016     1.00   c   0.121936318
1/6/2016     1.00   c   0.170198128
1/7/2016     1.00   c   0.735872415
1/8/2016     1.00   c   0.542361912
1/4/2016     2.00   a   0.723769247
1/5/2016     2.00   a   0.305570257
1/6/2016     2.00   b   0.47461605
1/7/2016     2.00   b   0.173702623
1/8/2016     2.00   c   0.969260251
1/9/2016     2.00   c   0.017170798

在 excel 中,我可以使用数据透视表来生成以下内容:

In excel, I can use a pivot table to produce the following:

使用数据"的excel数据透视表

在 python 中执行以下操作似乎是合理的:

It seems reasonable to do the following in python:

data.pivot(index='Date',columns = ['Stage','SubStage'],values = 'Value')

但这会产生:

KeyError: 'Level Stage not found'

是什么?

推荐答案

你想要的是 .pivot_table,而不是 .pivot.

You want .pivot_table, not .pivot.

import pandas
from io import StringIO

x = StringIO("""\
Date    Stage   SubStage    Value
12/31/2015   1.00   a   0.896882891
1/1/2016     1.00   a   0.0458843
1/2/2016     1.00   a   0.126805588
1/3/2016     1.00   b   0.615824461
1/4/2016     1.00   b   0.245092069
1/5/2016     1.00   c   0.121936318
1/6/2016     1.00   c   0.170198128
1/7/2016     1.00   c   0.735872415
1/8/2016     1.00   c   0.542361912
1/4/2016     2.00   a   0.723769247
1/5/2016     2.00   a   0.305570257
1/6/2016     2.00   b   0.47461605
1/7/2016     2.00   b   0.173702623
1/8/2016     2.00   c   0.969260251
1/9/2016     2.00   c   0.017170798
""")

df = pandas.read_table(x, sep='\s+')
xtab = df.pivot_table(index='Date', columns=['Stage','SubStage'], values='Value')
print(xtab.to_string(na_rep='--'))

这给了我:

Stage            1.0                           2.0                    
SubStage           a         b         c         a         b         c
Date                                                                  
1/1/2016    0.045884        --        --        --        --        --
1/2/2016    0.126806        --        --        --        --        --
1/3/2016          --  0.615824        --        --        --        --
1/4/2016          --  0.245092        --  0.723769        --        --
1/5/2016          --        --  0.121936  0.305570        --        --
1/6/2016          --        --  0.170198        --  0.474616        --
1/7/2016          --        --  0.735872        --  0.173703        --
1/8/2016          --        --  0.542362        --        --  0.969260
1/9/2016          --        --        --        --        --  0.017171
12/31/2015  0.896883        --        --        --        --        --

这篇关于使用枢轴的 Pandas KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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