In PANDAS, how to get the index of a known value?

前端 未结 5 567
一生所求
一生所求 2021-01-30 01:41

If we have a known value in a column, how can we get its index-value? For example:

In [148]: a = pd.DataFrame(np.arange(10).reshape(5,2),columns=[\'c1\',\'c2\'         


        
相关标签:
5条回答
  • 2021-01-30 02:23

    There might be more than one index map to your value, it make more sense to return a list:

    In [48]: a
    Out[48]: 
       c1  c2
    0   0   1
    1   2   3
    2   4   5
    3   6   7
    4   8   9
    
    In [49]: a.c1[a.c1 == 8].index.tolist()
    Out[49]: [4]
    
    0 讨论(0)
  • 2021-01-30 02:27

    To get the index by value, simply add .index[0] to the end of a query. This will return the index of the first row of the result...

    So, applied to your dataframe:

    In [1]: a[a['c2'] == 1].index[0]     In [2]: a[a['c1'] > 7].index[0]   
    Out[1]: 0                            Out[2]: 4                         
    

    Where the query returns more than one row, the additional index results can be accessed by specifying the desired index, e.g. .index[n]

    In [3]: a[a['c2'] >= 7].index[1]     In [4]: a[(a['c2'] > 1) & (a['c1'] < 8)].index[2]  
    Out[3]: 4                            Out[4]: 3 
    
    0 讨论(0)
  • 2021-01-30 02:29

    Using the .loc[] accessor:

    In [25]: a.loc[a['c1'] == 8].index[0]
    Out[25]: 4
    

    Can also use the get_loc() by setting 'c1' as the index. This will not change the original dataframe.

    In [17]: a.set_index('c1').index.get_loc(8)
    Out[17]: 4
    
    0 讨论(0)
  • 2021-01-30 02:29

    The other way around using numpy.where() :

    import numpy as np
    import pandas as pd
    
    In [800]: df = pd.DataFrame(np.arange(10).reshape(5,2),columns=['c1','c2'])
    
    In [801]: df
    Out[801]: 
       c1  c2
    0   0   1
    1   2   3
    2   4   5
    3   6   7
    4   8   9
    
    In [802]: np.where(df["c1"]==6)
    Out[802]: (array([3]),)
    
    In [803]: indices = list(np.where(df["c1"]==6)[0])
    
    In [804]: df.iloc[indices]
    Out[804]: 
       c1  c2
    3   6   7
    
    In [805]: df.iloc[indices].index
    Out[805]: Int64Index([3], dtype='int64')
    
    In [806]: df.iloc[indices].index.tolist()
    Out[806]: [3]
    
    0 讨论(0)
  • 2021-01-30 02:33

    I think this may help you , both index and columns of the values.

    value you are looking for is not duplicated:

    poz=matrix[matrix==minv].dropna(axis=1,how='all').dropna(how='all')
    value=poz.iloc[0,0]
    index=poz.index.item()
    column=poz.columns.item()
    

    you can get its index and column

    duplicated:

    matrix=pd.DataFrame([[1,1],[1,np.NAN]],index=['q','g'],columns=['f','h'])
    matrix
    Out[83]: 
       f    h
    q  1  1.0
    g  1  NaN
    poz=matrix[matrix==minv].dropna(axis=1,how='all').dropna(how='all')
    index=poz.stack().index.tolist()
    index
    Out[87]: [('q', 'f'), ('q', 'h'), ('g', 'f')]
    

    you will get a list

    0 讨论(0)
提交回复
热议问题