pandas报错:KeyError:
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pandas报错:KeyError:相关的知识,希望对你有一定的参考价值。
参考技术A 使用pandas读取.csv文件,使用df[\'列名\']的时候,总是报如下错误。
.csv文件如下:
Traceback (most recent call last):
File "C:\\Users\\zhangheng\\anaconda3\\envs\\py36\\lib\\site-packages\\pandas\\core\\indexes\\base.py", line 2393, in get_loc
return self._engine.get_loc(key)
File "pandas_libs\\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas_libs\\index.c:5239)
File "pandas_libs\\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas_libs\\index.c:5085)
File "pandas_libs\\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas_libs\\hashtable.c:20405)
File "pandas_libs\\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas_libs\\hashtable.c:20359)
KeyError: \'交易日期\'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Quant/Practice/Code/xbx_stock_2019/program/pandas基础/5_缺失处理.py", line 25, in <module>
index = df[df[\'交易日期\'].isin([\'2019-03-01\'])].index
File "C:\\Users\\zhangheng\\anaconda3\\envs\\py36\\lib\\site-packages\\pandas\\core\\frame.py", line 2062, in getitem
return self._getitem_column(key)
File "C:\\Users\\zhangheng\\anaconda3\\envs\\py36\\lib\\site-packages\\pandas\\core\\frame.py", line 2069, in _getitem_column
return self._get_item_cache(key)
File "C:\\Users\\zhangheng\\anaconda3\\envs\\py36\\lib\\site-packages\\pandas\\core\\generic.py", line 1534, in _get_item_cache
values = self._data.get(item)
File "C:\\Users\\zhangheng\\anaconda3\\envs\\py36\\lib\\site-packages\\pandas\\core\\internals.py", line 3590, in get
loc = self.items.get_loc(item)
File "C:\\Users\\zhangheng\\anaconda3\\envs\\py36\\lib\\site-packages\\pandas\\core\\indexes\\base.py", line 2395, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas_libs\\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas_libs\\index.c:5239)
File "pandas_libs\\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas_libs\\index.c:5085)
File "pandas_libs\\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas_libs\\hashtable.c:20405)
File "pandas_libs\\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas_libs\\hashtable.c:20359)
KeyError: \'交易日期\'
Process finished with exit code 1
import pandas as pd # 将pandas作为第三方库导入,我们一般为pandas取一个别名叫做pd
pd.set_option(\'expand_frame_repr\', False) # 当列太多时清楚展示
df = pd.read_csv(
r\'D:\\Quant\\Practice\\Code\\xbx_stock_2019\\data\\a_stock_201903.csv\',
encoding=\'gbk\',
skiprows=1
)
在导入数据时,skiprows=1,即即读取.csv文件时,跳过了第一行的列名,没有读取列名,你使用 df[\'列名\'] 就会报错
Pandas - KeyError: '[] not in index' 训练 Keras 模型时
【中文标题】Pandas - KeyError: \'[] not in index\' 训练 Keras 模型时【英文标题】:Pandas - KeyError: '[] not in index' when training a Keras modelPandas - KeyError: '[] not in index' 训练 Keras 模型时 【发布时间】:2018-01-10 18:22:07 【问题描述】:我正在尝试根据我的数据集中的部分特征来训练 Keras 模型。我已经加载了数据集并提取了如下特征:
train_data = pd.read_csv('../input/data.csv')
X = train_data.iloc[:, 0:30]
Y = train_data.iloc[:,30]
# Code for selecting the important features automatically (removed) ...
# Selectintg important features 14,17,12,11,10,16,18,4,9,3
X = train_data.reindex(columns=['V14','V17','V12','V11','V10','V16','V18','V4','V9','V3'])
print(X.shape[1]) # -> 10
但是当我调用 fit 方法时:
# Fit the model
history = model.fit(X, Y, validation_split=0.33, epochs=10, batch_size=10, verbose=0, callbacks=[early_stop])
我收到以下错误:
KeyError: '[3 2 5 1 0 4] not in index'
我错过了什么?
【问题讨论】:
检查this thread。 【参考方案1】:keras
期望模型输入是 numpy
数组 - 而不是 pandas.DataFrame
s。试试:
X = train_data.iloc[:, 0:30].as_matrix()
Y = train_data.iloc[:,30].as_matrix()
由于as_matrix
方法将pandas.DataFrame
转换为numpy.array
。
【讨论】:
以上是关于pandas报错:KeyError:的主要内容,如果未能解决你的问题,请参考以下文章
Pandas读取csv文件报错UnicodeDecodeError
python_执行报错_No module named 'pandas'
[解决问题] pandas读取csv文件报错OSError解决方案
解决错误:pandas.read_csv() 报错 OSError: Initializing from file failed