简体   繁体   English

尝试绘制熊猫Pivot_table时发生KeyError

[英]KeyError while trying to plot a pandas pivot_table

I build a pivot_table out of a dataframe to get numbers distribution between years across states: 我在数据框中构建了一个数据透视表,以获取各州各年之间的数字分布:

data['issue_d'] = pd.to_datetime(data['issue_d'])
data['issue_year'] = data['issue_d'].dt.year
data_table_years = data.pivot_table(data,index='addr_state', columns='issue_year')['loan_amnt']

And it gives me nice outcome: 它给了我很好的结果:

issue_year       2007.0     2008.0      2009.0      2010.0      2011.0
addr_state                  
AK               nan        12,300.00   12,480.56   10,666.67   13,975.98
AL               6,855.56   9,986.25    8,787.95    9,850.96    12,841.84
AR               2,533.33   9,018.75    10,479.17   9,892.19    10,848.63

But when I try to build a lines plot out of it it will build the plot but the Series are the issue_year (I need the addr_state instead so that issue years are on the X axis) 但是,当我尝试从中构建线图时,它将构建图,但是系列是issue_year(我需要使用addr_state,以便发布年份在X轴上)

But when I try to .plot(x='addr_state') I get KeyError: 'addr_state' 但是当我尝试.plot(x='addr_state')我得到KeyError: 'addr_state'

Is there a way to set the plot to use the right Series? 有没有办法设置绘图以使用正确的序列?

您为什么不移调呢?:

data_table_years.T.plot()

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

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