请问pandas中出现keyerror要怎样解决呢?
程序:#!/usr/bin/env Python
#coding=utf-8
import numpy as np
import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt
from pyhht.emd import EMD
from pyhht.visualization import plot_imfs
#载入时间序列数据
data = pd.read_csv(r'C:\Users\Administrator\Desktop\课程\赵贞玉\数据\周材料.csv',encoding='utf-8')
print(data)
#EMD经验模态分解
decomposer = EMD(data)
imfs = decomposer.decompose()
#绘制分解图
plot_imfs(data,imfs,data.index)
#保存IMFs
arr = np.vstack((imfs,data))
dataframe = pd.DataFrame(arr.T)
dataframe.to_csv(r'C:\Users\Administrator\Desktop\课程\赵贞玉\数据\周材料imfs.csv',index=None,columns=None)
错误提示:
closing data
0 3148.32
1 3561.29
2 3546.21
3 3614.76
-- --
513 2521.69
514 2471.80
515 2450.03
Traceback (most recent call last):
File "D:\python\lib\site-packages\pandas\core\indexes\base.py", line 2891, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\python\program\EMD.py", line 15, in <module>
decomposer= EMD(data)
File "D:\python\lib\site-packages\pandas\core\frame.py", line 2902, in __getitem__
indexer = self.columns.get_loc(key)
File "D:\python\lib\site-packages\pandas\core\indexes\base.py", line 2893, in get_loc
raise keyerror(key) from err
KeyError:0
应该是因为第十五行代码 data找不到键导致报错 pandas的索引是列索引,所以报错的那一行应该改成:
plot_imfs(data.iloc, imfs, data.index)
iloc属性的用法是:
data.iloc[行, 列]
页:
[1]