python - 每当我尝试将值放入训练数据时,它都会显示关键错误
问题描述
import pandas as pd
fruits = pd.read_csv('fruit_data_with_colors.txt')
fruits.head()
X= fruits['mass','height','width']
> Error - KeyError Traceback (most
> recent call last)
> C:\Python\Python37\ANACONDA3\lib\site-packages\pandas\core\indexes\base.py
> in get_loc(self, key, method, tolerance) 2656 try:
> -> 2657 return self._engine.get_loc(key) 2658 except KeyError:
>
> pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
>
> pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
>
> pandas/_libs/hashtable_class_helper.pxi in
> pandas._libs.hashtable.PyObjectHashTable.get_item()
>
> pandas/_libs/hashtable_class_helper.pxi in
> pandas._libs.hashtable.PyObjectHashTable.get_item()
>
> KeyError: ('mass', 'height', 'width')
>
> During handling of the above exception, another exception occurred:
>
> KeyError Traceback (most recent call
> last) <ipython-input-13-f018d289c28d> in <module>
> ----> 1 X= fruits['mass','height','width']
>
> C:\Python\Python37\ANACONDA3\lib\site-packages\pandas\core\frame.py in
> __getitem__(self, key) 2925 if self.columns.nlevels > 1: 2926 return self._getitem_multilevel(key)
> -> 2927 indexer = self.columns.get_loc(key) 2928 if is_integer(indexer): 2929 indexer = [indexer]
>
> C:\Python\Python37\ANACONDA3\lib\site-packages\pandas\core\indexes\base.py
> in get_loc(self, key, method, tolerance) 2657
> return self._engine.get_loc(key) 2658 except KeyError:
> -> 2659 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2660
> indexer = self.get_indexer([key], method=method, tolerance=tolerance)
> 2661 if indexer.ndim > 1 or indexer.size > 1:
>
> pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
>
> pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
>
> pandas/_libs/hashtable_class_helper.pxi in
> pandas._libs.hashtable.PyObjectHashTable.get_item()
>
> pandas/_libs/hashtable_class_helper.pxi in
> pandas._libs.hashtable.PyObjectHashTable.get_item()
>
> KeyError: ('mass', 'height', 'width')
解决方案
尝试将最后一行更改为
X = fruits[['mass','height','width']]
推荐阅读
- ssis - SQL Server Integration Services 进程挂在服务器上
- swift - 基于 Swift 中的 creationDate 从库中获取所有照片 [更快的方式?]
- codeigniter-3 - CodeIgniter MySQL 选择和查询
- excel - 删除单个工作簿中每个工作表的错误行
- java - 如果服务器未读取,则跳过 Java 客户端套接字写入
- json - System.Collections.Generic.List`1[System.String]" JSON 错误
- ios - UIAppearance 不适用于可重用的集合视图单元格
- asp.net-core - 我如何将asp.net mvc核心应用程序托管到linux服务器应用程序是在window平台上开发的
- reactjs - 在反应路由器中更改路径而不导致重新渲染?
- java - maven 依赖项升级/更改后 shiro.ini 配置失败