Retrieve name of column from its Index in Pandas

后端 未结 1 590
生来不讨喜
生来不讨喜 2020-12-28 14:01

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

相关标签:
1条回答
  • 2020-12-28 14:12

    I think you need index columns names by position (python counts from 0, so for fourth column need 3):

    colname = df.columns[pos]
    

    Sample:

    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')
    
    0 讨论(0)
提交回复
热议问题