【发布时间】:2022-08-06 22:22:11
【问题描述】:
尝试在单个 df[ _ , _ ] 表单中打印数据帧时出现错误。下面是代码行
#Data Frames code
import numpy as np
import pandas as pd
randArr = np.random.randint(0,100,20).reshape(5,4)
df =pd.DataFrame(randArr,np.arange(101,106,1),[\'PDS\',\'Algo\',\'SE\',\'INS\'])
print(df[\'PDS\',\'SE\'])
错误:
Traceback (most recent call last): File \"C:\\Users\\subro\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\pandas\\core\\indexes\\base.py\", line 3621, in get_loc return self._engine.get_loc(casted_key) File \"pandas\\_libs\\index.pyx\", line 136, in pandas._libs.index.IndexEngine.get_loc File \"pandas\\_libs\\index.pyx\", line 163, in pandas._libs.index.IndexEngine.get_loc File \"pandas\\_libs\\hashtable_class_helper.pxi\", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item File \"pandas\\_libs\\hashtable_class_helper.pxi\", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: (\'PDS\', \'SE\')
上述异常是以下异常的直接原因:
Traceback (most recent call last): File \"D:\\Education\\4th year\\1st sem\\Machine Learning Lab\\1st Lab\\python\\pandas\\pdDataFrame.py\", line 11, in <module> print(df[\'PDS\',\'SE\']) File \"C:\\Users\\subro\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\pandas\\core\\frame.py\", line 3505, in __getitem__ indexer = self.columns.get_loc(key) File \"C:\\Users\\subro\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\pandas\\core\\indexes\\base.py\", line 3623, in get_loc raise KeyError(key) from err KeyError: (\'PDS\', \'SE\')
标签: python pandas dataframe numpy random