Return the column name(s) for a specific value in a pandas dataframe

后端 未结 4 1331
孤独总比滥情好
孤独总比滥情好 2020-12-08 23:40

where I have found this option in other languages such as R or SQL but I am not quite sure how to go about this in Pandas.

So I have a file with 1262 columns and 1 r

相关标签:
4条回答
  • 2020-12-08 23:45

    You can use data frame slicing and then get the columns names:

    df.ix[:,df.loc[0] == 38.15].columns
    

    output:

    Index([u'col7'], dtype='object')
    
    0 讨论(0)
  • 2020-12-08 23:50

    just for the sake of throwing something a bit different into the ring:

    row = df.iloc[0]
    row.reset_index().set_index(0).loc[38.15]
    
    0 讨论(0)
  • 2020-12-08 23:55

    Let's say we have this df . Checking only the first three rows of the df we want to get the name of the column where the specific value is 5.

    df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))
        df.head(3)
    

    We can do this:

    In[61]:
    for index, row in df[:3].iterrows():
        for i in range(len(df.columns)): 
            if row[i] == 5:
                print(row.index[i])
    Out[61]:
    'D'   
    
    0 讨论(0)
  • 2020-12-09 00:09

    Seeing as you only have a single row then you can call iloc[0] on the result and use this to mask the columns:

    In [47]:
    df.columns[(df == 38.15).iloc[0]]
    
    Out[47]:
    Index(['col7'], dtype='object')
    

    Breaking down the above:

    In [48]:
    df == 38.15
    
    Out[48]:
                 Date   col1   col2   col3   col4   col5   col6  col7
    01/01/2016  False  False  False  False  False  False  False  True
    
    In [49]:
    (df == 38.15).iloc[0]
    
    Out[49]:
    Date    False
    col1    False
    col2    False
    col3    False
    col4    False
    col5    False
    col6    False
    col7     True
    Name: 01/01/2016, dtype: bool
    

    You can also use idxmax with param axis=1:

    In [52]:
    (df == 38.15).idxmax(axis=1)[0]
    
    Out[52]:
    'col7'
    
    0 讨论(0)
提交回复
热议问题