我在pandas中有一个数据框,看起来像这样:
100 200 300 400
0 1 1 0 1
1 1 1 1 0
我要做的是从这个数据框中选择特定的列。但当我尝试以下代码时(df_矩阵是顶部显示的数据帧):
^{pr2}$它不起作用,从我能看出的是因为它是一个整数。我试着用str(100)来强迫它,但还是犯了同样的错误:
File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item
TypeError: an integer is required
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "A:\python project\venv\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 164, in pandas._libs.index.IndexEngine.get_loc
KeyError: '100'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item
TypeError: an integer is required
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "A:/python project/testing/testing4.py", line 42, in <module>
intermediary_df = df_matrix["100"]
File "A:\python project\venv\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
return self._getitem_column(key)
File "A:\python project\venv\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
return self._get_item_cache(key)
File "A:\python project\venv\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
values = self._data.get(item)
File "A:\python project\venv\lib\site-packages\pandas\core\internals.py", line 4115, in get
loc = self.items.get_loc(item)
File "A:\python project\venv\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 164, in pandas._libs.index.IndexEngine.get_loc
KeyError: '100'
有人知道怎么避开这个吗?谢谢!在
编辑1:
在尝试使用intermediary_df = df_matrix[100]
后,它如期工作。顺便说一句,如果其他人面临此问题,并且希望同时选择多个列,您可以使用:
intermediary_df = df_matrix[[100, 300]]
输出将是:
100 300
0 1 0
1 1 1
在本例中,只需使用下面的内容,因为您的列是
int
。在如果要将列作为
str
访问,请使用:df.columns = [str(x) for x in df.columns]
然后呢
df['100']
输出
^{pr2}$我想你的列类型是整数, 但如果不使用
DataFrame.loc
尝试此操作示例:
或者
^{pr2}$相关问题 更多 >
编程相关推荐