Python 在panda的数据框中选择具有整数标题的列
我在pandas中有一个数据帧,如下所示:Python 在panda的数据框中选择具有整数标题的列,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我在pandas中有一个数据帧,如下所示: 100 200 300 400 0 1 1 0 1 1 1 1 1 0 我要做的是从这个数据框中选择特定的列。但是,当我尝试以下代码时,df_矩阵就是顶部显示的数据帧: intermediary_df = df_matrix["100"] 它不起作用,从我所知道的是,因为它是一个整数。我试图用str100强制执行,但给出了与以前相同的错误: File "pandas\_libs\hasht
100 200 300 400
0 1 1 0 1
1 1 1 1 0
我要做的是从这个数据框中选择特定的列。但是,当我尝试以下代码时,df_矩阵就是顶部显示的数据帧:
intermediary_df = df_matrix["100"]
它不起作用,从我所知道的是,因为它是一个整数。我试图用str100强制执行,但给出了与以前相同的错误:
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'
输出将是:
100 300
0 1 0
1 1 1
我认为您的列类型是整数, 但如果不是这样,请尝试使用DataFrame.loc 例如: 或
我认为您的列类型是整数, 但如果不是这样,请尝试使用DataFrame.loc 例如: 或
在本例中,只需使用以下内容,因为您的列是int 如果希望列作为str访问,请使用: df.columns=[df.columns中x的strx] 然后 df['100'] 输出
0 1
1 1
Name: 100, dtype: int64
在本例中,只需使用以下内容,因为您的列是int 如果希望列作为str访问,请使用: df.columns=[df.columns中x的strx] 然后 df['100'] 输出
0 1
1 1
Name: 100, dtype: int64
打印列df_matrix.columns,检查它是int还是strTry intermediate_df=df_matrix[100]?@W-B它似乎是整数。控制台Int64Index[100200300400]的输出,dtype='int64'所以试试,df_matrix[100]@harvpan它现在工作了。非常感谢。打印列df_matrix.columns,检查它是int还是strTry intermediate_df=df_matrix[100]?@W-B它似乎是整数。控制台Int64Index[100200300400]的输出,dtype='int64'所以试试,df_matrix[100]@harvpan它现在工作了。非常感谢。代码不起作用,但使用中间层[100]使其起作用代码不起作用,但使用中间层[100]使其起作用您知道如何在带有整数标题的列上应用过滤器吗?我试过df_matrix[df_matrix.100==1],但结果是错误的……只需使用df_matrix.loc[df_matrix[100]==1]就可以了@阿德里安。祝你好运您知道如何在带有整数标题的列上应用筛选器吗?我试过df_matrix[df_matrix.100==1],但结果是错误的……只需使用df_matrix.loc[df_matrix[100]==1]就可以了@阿德里安。祝你好运
intermediary_df = df_matrix.iloc[:,0]
intermediary_df = df_matrix[100]`
0 1
1 1
Name: 100, dtype: int64