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

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

提交回复
热议问题