Getting the integer index of a Pandas DataFrame row fulfilling a condition?

前端 未结 3 2034
野性不改
野性不改 2020-12-04 17:37

I have the following DataFrame:

   a  b  c
b
2  1  2  3
5  4  5  6

As you can see, column b is used as an index. I want to get

相关标签:
3条回答
  • 2020-12-04 18:15

    With Index.get_loc and general condition:

    >>> import pandas as pd
    >>> import numpy as np
    
    
    >>> df = pd.DataFrame(np.arange(1,7).reshape(2,3),
                      columns = list('abc'),
                      index=pd.Series([2,5], name='b'))
    >>> df
       a  b  c
    b
    2  1  2  3
    5  4  5  6
    >>> df.index.get_loc(df.index[df['b'] == 5][0])
    1
    
    0 讨论(0)
  • 2020-12-04 18:28

    You could use np.where like this:

    import pandas as pd
    import numpy as np
    df = pd.DataFrame(np.arange(1,7).reshape(2,3),
                      columns = list('abc'), 
                      index=pd.Series([2,5], name='b'))
    print(df)
    #    a  b  c
    # b         
    # 2  1  2  3
    # 5  4  5  6
    print(np.where(df.index==5)[0])
    # [1]
    print(np.where(df['c']==6)[0])
    # [1]
    

    The value returned is an array since there could be more than one row with a particular index or value in a column.

    0 讨论(0)
  • 2020-12-04 18:35

    Use Index.get_loc instead.

    Reusing @unutbu's set up code, you'll achieve the same results.

    >>> import pandas as pd
    >>> import numpy as np
    
    
    >>> df = pd.DataFrame(np.arange(1,7).reshape(2,3),
                      columns = list('abc'),
                      index=pd.Series([2,5], name='b'))
    >>> df
       a  b  c
    b
    2  1  2  3
    5  4  5  6
    >>> df.index.get_loc(5)
    1
    
    0 讨论(0)
提交回复
热议问题