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

前端 未结 5 582
一生所求
一生所求 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: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
    

提交回复
热议问题