从Pandas中的Index中检索列的名称 [英] Retrieve name of column from its Index in Pandas
本文介绍了从Pandas中的Index中检索列的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个pandas数据帧和该数据帧的numpy值数组。
我有一个特定列的索引,我已经有一个重要值的行索引。现在我需要从我的数据帧中获取该特定值的列名。
I have a pandas dataframe and a numpy array of values of that dataframe. I have the index of a specific column and I already have the row index of an important value. Now I need to get the column name of that particular value from my dataframe.
在搜索完文档后,我发现我可以做相反但不是我的想。
After searching through the documentations, I found out that I can do the opposite but not what I want.
推荐答案
我认为你需要按位置索引列名(python从 0开始计算
,因此对于第四列需要 3
):
I think you need index columns names by position (python counts from 0
, so for fourth column need 3
):
colname = df.columns[pos]
样本:
df = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9],
'D':[1,3,5],
'E':[5,3,6],
'F':[7,4,3]})
print (df)
A B C D E F
0 1 4 7 1 5 7
1 2 5 8 3 3 4
2 3 6 9 5 6 3
pos = 3
colname = df.columns[pos]
print (colname)
D
pos = [3,5]
colname = df.columns[pos]
print (colname)
Index(['D', 'F'], dtype='object')
这篇关于从Pandas中的Index中检索列的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文