How to get scalar value on a cell using conditional indexing

后端 未结 2 755
灰色年华
灰色年华 2020-12-09 07:27

I have the dataframe shown below. I need to get the scalar value of column B, dependent on the value of A (which is a variable in my script). I\'m trying the loc() function

相关标签:
2条回答
  • 2020-12-09 07:55

    elaborating on @ShineZhang comment:

    x.set_index('A').at[2, 'B']

    6

    pd.__version__

    u'0.22.0'

    0 讨论(0)
  • 2020-12-09 08:04

    First of all, you're better off accessing both the row and column indices from the .loc:

    x.loc[x['A'] == 2, 'B']
    

    Second, you can always get at the underlying numpy matrix using .values on a series or dataframe:

    In : x.loc[x['A'] == 2, 'B'].values[0]
    Out: 6
    

    Finally, if you're not interested in the original question's "conditional indexing", there are also specific accessors designed to get a single scalar value from a DataFrame: dataframe.at[index, column] or dataframe.iat[i, j] (these are similar to .loc[] and .iloc[] but designed for quick access to a single value).

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