简体   繁体   English

日历数据透视表熊猫keyerror

[英]Calendar pivot table pandas keyerror

I have a time series of values by day, so, something like this我每天都有一个时间序列的值,所以,像这样

date          value
2020-01-01    50000
2020-01-02    50130
...
2020-10-18    48763

The column 'date'is used as index and parsed when importing the csv 'date' 列用作索引并在导入 csv 时进行解析

I'd like to put those values into a pivot table like this using pandas我想使用熊猫将这些值放入这样的数据透视表中

       2018     2019     2020
------------------------------
jan   50000    32420    21488
feb   48237    38240    98783
mar   51682    21984    21984
apr   49956    14878    14847

where the data by month/year are aggregated by sum of the values taken into the specified month I'm using the libraries pandas and calendar and the function .pivot_table其中按月/年的数据按指定月份中所取值的总和聚合我正在使用库熊猫和日历以及函数 .pivot_table

Looking at what they suggest in this guide and the author uses these lines of code查看他们在本指南中的建议,作者使用这些代码行

import calendar
all_month_year_df = pd.pivot_table(df, values="Open",
                               index=["month"],
                               columns=["year"],
                               fill_value=0,
                               margins=True)
named_index = [[calendar.month_abbr[i] if isinstance(i, int) else i for i in 
list(all_month_year_df.index)]] # name months
all_month_year_df = all_month_year_df.set_index(named_index)
all_month_year_df

but all I get is a KeyError for 'month' and I can't figure out what is the reason但我得到的只是“月”的 KeyError,我不知道是什么原因

Can you help me figure out why?你能帮我找出原因吗? Where is this code wrong?这段代码哪里错了? Using python 3.8.3 64 bit with vscode on ubuntu 20.04 it this info helps在 ubuntu 20.04 上使用带有 vscode 的 python 3.8.3 64 位,这个信息有帮助

Thank you谢谢

Your input dataframe only has two columns, data, and value.您的输入数据框只有两列、数据和值。

You need to put in two columns for month and year.您需要为月份和年份放入两列。

something like就像是

df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year

The problem is you don't have a month or year column yet (you only have a date column), so you need to create the columns first based on the date column as follows:问题是您还没有月份或年份列(您只有一个日期列),因此您需要首先根据日期列创建列,如下所示:

df['month'] = df.date.dt.month
df['year'] = df.date.dt.year

this wont work because you are using 'Date' column as Index so this wont solve your issue.. simply replace index and column with this这行不通,因为您使用“日期”列作为索引,因此这不会解决您的问题..只需用此替换索引和列

index=[df.index.month], columns=[df.index.year]

and since you have not cleaned your dataset use并且由于您尚未清理数据集使用

margins=False

this will definately work and its too short also inplace of making new column of month and year.这肯定会起作用,而且它也太短了,无法制作新的月份和年份列。

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

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