鱼C论坛

 找回密码
 立即注册
查看: 7790|回复: 2

请问pandas中出现keyerror要怎样解决呢?

[复制链接]
发表于 2020-11-18 00:56:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
程序:
#!/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[0])               
imfs = decomposer.decompose()
#绘制分解图
plot_imfs(data[0],imfs,data.index)
#保存IMFs
arr = np.vstack((imfs,data[0]))
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[0])
  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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-18 06:10:42 | 显示全部楼层


应该是因为第十五行代码 data[0]找不到键导致报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-30 16:27:12 | 显示全部楼层
pandas的索引是列索引,所以报错的那一行应该改成:
plot_imfs(data.iloc[0], imfs, data.index)
iloc属性的用法是:
data.iloc[行, 列]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-24 19:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表