简体   繁体   English

枢纽分析表在Pandas中传回主要错误

[英]Pivot table returns a key error in Pandas

I have a dataframe called performanceData that looks like: 我有一个名为performanceData的数据框,看起来像:

                         id  settle_price  settle_price_acc     pos    trade  \
date                                                                           
2016-01-04  BBG.XTKS.9716.S     14.096973         14.096973  8100.0  -1700.0   
2016-01-04  BBG.XTKS.9065.S      4.460498          4.460498  4000.0  20000.0   
2016-01-04  BBG.XTKS.7966.S     17.599029         17.599029  -500.0  27700.0   
2016-01-04  BBG.XTKS.3774.S     19.514999         19.514999     0.0    200.0   
2016-01-04  BBG.XTKS.5110.S     15.143828         15.143828  -200.0      0.0   

            nominal_posn_size  abs_nominal_posn_size  abs_nominal_trade_size  
date                                                                          
2016-01-04      114185.481138          114185.481138            23964.854066  
2016-01-04       17841.990960           17841.990960            89209.954800  
2016-01-04       -8799.514730            8799.514730           487493.116042  
2016-01-04           0.000000               0.000000             3902.999806  
2016-01-04       -3028.765682            3028.765682                0.000000

The dataframe is large and has dates for the last 10 years (above is the head). 数据框很大,并且具有最近10年的日期(上方是头部)。 I am trying to create a pivot table on date (for example summing all of the abs_nominal_trade_size values for each date. However I get a key error 我试图在日期上创建数据透视表(例如,将每个日期的所有abs_nominal_trade_size值相加。但是我遇到了关键错误

KeyError: 'date'

When I try and pivot on the date using the following code: 当我尝试使用以下代码确定日期时:

 e = performanceData.pivot_table(index='date', columns=['abs_nominal_trade_size','abs_nominal_posn_size','nominal_posn_size'], values='Value')

Let's try: 我们试试吧:

performanceData.reset_index()\
               .pivot_table(index='date', 
                            values=['abs_nominal_trade_size',
                                     'abs_nominal_posn_size',
                                     'nominal_posn_size'],
                             aggfunc='sum')

Output: 输出:

            abs_nominal_posn_size  abs_nominal_trade_size  nominal_posn_size
date                                                                        
2016-01-04           143855.75251           604570.924714      120199.191686

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM