从Pandas中的索引中检索列的名称

Jat*_*ola 15 python indexing numpy pandas

我有一个pandas数据帧和该数据帧的numpy值数组.我有一个特定列的索引,我已经有一个重要值的行索引.现在我需要从我的数据帧中获取该特定值的列名.

在搜索完文档后,我发现我可以做相反但不是我想要的.

jez*_*ael 25

我认为您需要按位置索引列名称(python计数来自0,因此需要第四列3):

colname = df.columns[pos]
Run Code Online (Sandbox Code Playgroud)

样品:

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
Run Code Online (Sandbox Code Playgroud)
pos = [3,5]
colname = df.columns[pos]
print (colname)
Index(['D', 'F'], dtype='object')
Run Code Online (Sandbox Code Playgroud)