python keyerror not in index_python – 带有索引的Pandas Plot导致’KeyError []不在索引中...

本文档描述了在使用Pandas对带有索引的数据框进行绘图时遇到'KeyError []不在索引中'的问题。作者尝试为不同植物的Output创建与特定变压器[Trafo1]的子图,但在调用plot方法时遇到了错误。错误出现在尝试从数据框中获取'Trafo1'列时,提示给定的值不在索引中。
摘要由CSDN通过智能技术生成

我是

Python中Pandas概念的新手.通常情节不是问题.但是,我现在面临的是包含索引的数据框.不知何故什么都没有了.

我想要实现的目标:

为每个列[Plant1,Plant2,Plant3]创建一个特定柱[Trafo1]的子图.

这是我的代码:

import numpy as np

import datetime

import numpy as np

import matplotlib

import matplotlib.pyplot as plt

import pandas as pd

import os

# Create the sample data

plant1 = {'Date' : pd.date_range('1/1/2011', periods=10, freq='D'),

'Plant' : pd.Series(["Plant1"]*10),

'Output' : pd.Series(abs(np.random.randn(10)))}

plant2 = {'Date' : pd.date_range('1/3/2011', periods=10, freq='D'),

'Plant' : pd.Series(["Plant2"]*10),

'Output' : pd.Series(abs(np.random.randn(10)))}

plant3 = {'Date' : pd.date_range('1/5/2011', periods=10, freq='D'),

'Plant' : pd.Series(["Plant3"]*10),

'Output' : pd.Series(abs(np.random.randn(10)))}

trafo1 = {'Date' : pd.date_range('1/5/2011', periods=10, freq='D'),

'Plant' : pd.Series(["Trafo1"]*10),

'Output' : pd.Series(abs(np.random.randn(10)))}

trafo2 = {'Date' : pd.date_range('1/5/2011', periods=10, freq='D'),

'Plant' : pd.Series(["Trafo2"]*10),

'Output' : pd.Series(abs(np.random.randn(10)))}

df_plant_1 = pd.DataFrame(plant1)

df_plant_2 = pd.DataFrame(plant2)

df_plant_3 = pd.DataFrame(plant3)

df_trafo_1 = pd.DataFrame(trafo1)

df_trafo_2 = pd.DataFrame(trafo2)

sample = pd.concat([df_plant_1,df_plant_2,df_plant_3,df_trafo_1,df_trafo_2])

test = pd.pivot_table(sample, index='Date', columns='Plant', values='Output')

test = test.fillna(method='pad')

test = test.fillna(method='bfill')

# Draw the plots

matplotlib.style.use('ggplot')

cols = len(test.columns) - 1

fig, axes = plt.subplots(nrows=cols/2, ncols=2, figsize=(12, 4))

for column in test.iloc[:,:-1]:

test.plot(x=test[column], y=test['Trafo1'], title=column)

plt.gca().set_aspect('equal', adjustable='box')

plt.show()

导致以下错误输出:

runfile('C:/..../untitled12.py', wdir='C:/...')

Traceback (most recent call last):

File "", line 1, in

runfile('C:/Users/bjl/untitled12.py', wdir='C:/Users/bjl')

File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile

execfile(filename, namespace)

File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile

exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/bjl/untitled12.py", line 52, in

test.plot(x=test[column], y=test['Trafo1'], title=column)

File "C:\Anaconda2\lib\site-packages\pandas\tools\plotting.py", line 3671, in __call__

sort_columns=sort_columns, **kwds)

File "C:\Anaconda2\lib\site-packages\pandas\tools\plotting.py", line 2556, in plot_frame

**kwds)

File "C:\Anaconda2\lib\site-packages\pandas\tools\plotting.py", line 2370, in _plot

series = data[y].copy() # Don't modify

File "C:\Anaconda2\lib\site-packages\pandas\core\frame.py", line 1963, in __getitem__

return self._getitem_array(key)

File "C:\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2007, in _getitem_array

indexer = self.ix._convert_to_indexer(key, axis=1)

File "C:\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1150, in _convert_to_indexer

raise KeyError('%s not in index' % objarr[mask])

KeyError: '[ 1.20311253 1.20311253 1.20311253 1.20311253 1.20311253 0.32765014\n 1.65686117 2.58118029 0.58903059 0.13907876 0.59270297 0.27072611\n 0.50167366 1.0310578 ] not in index'

我不明白索引的问题是什么.我无法在线找到任何帮助,因为所有示例都没有索引.

我非常感谢你的帮助.这个错误对新手来说很神秘.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值