问题描述
所以我当时正在研究一些数据集,然后尝试使用熊猫进行分析,结果偶然发现了以下错误..,我的大脑呆滞了:(
这是引发异常的代码段
import pandas as pd
from sklearn.datasets import load_breast_cancer
X,y = load_breast_cancer(return_X_y=True)
data = pd.DataFrame(X)
data['class'] = y
data.head()
data.tail()
data.columns
print('length of data is',len(data))
data.shape
data.info()
这是错误引用
C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\Scripts\python.exe C:/Users/97150/PycharmProjects/EmbeddedLinux/AI/project.py
length of data is 569
Traceback (most recent call last):
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\indexes\base.py",line 2889,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 97,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: 30
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/97150/PycharmProjects/EmbeddedLinux/AI/project.py",line 42,in <module>
data.info()
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\frame.py",line 2587,in info
self,verbose,buf,max_cols,memory_usage,null_counts
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\io\formats\info.py",line 250,in info
self._verbose_repr(lines,ids,dtypes,show_counts)
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\io\formats\info.py",line 335,in _verbose_repr
dtype = dtypes[i]
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\series.py",line 882,in __getitem__
return self._get_value(key)
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\series.py",line 991,in _get_value
loc = self.index.get_loc(label)
File "C:\Users\97150\PycharmProjects\EmbeddedLinux\venv\lib\site-packages\pandas\core\indexes\base.py",line 2891,in get_loc
raise KeyError(key) from err
KeyError: 30
Process finished with exit code 1
注意:我正在使用PyCharm社区2020.2,并检查了更新等内容,
解决方法
所以,事实证明,熊猫表现得很奇怪。
从()
中删除data.info()
可以解决问题:)
您也可以尝试将verbose=True
和null_counts=True
参数传递给.info()
方法以显示结果(如果不想考虑空值,可以使用冗长的参数)
data.info(verbose=True,null_counts=True)
让我知道事情是否对您有用。