0

I want to get the value from the column "classification" of the second row.

I have : id = 'D1-0021'; LeftRight = 'R'

So with this information i want the result to the corresponding column of classification.

Can some explain with pandas how to make something like :

table.loc['D1-0021', where 'LeftRight' == R] get value['classification']

corresponding output : 'Benign'

       LeftRight  Age  number    abnormality classification subtype
id                                                                  
D1-0021         L   22       2  calcification         Benign     NaN
D1-0021         R   22       2  calcification         Benign     NaN
petezurich
  • 9,280
  • 9
  • 43
  • 57

2 Answers2

2

If you just want to get the data:

df.query('id=="D1-0021" & LeftRight=="R"')['classification']

Output:

id
D1-0021    Benign
Name: classification, dtype: object

And to get the string specifically, use .item():

df.query('id=="D1-0021" & LeftRight=="R"')['classification'].item()

Or:

# query to filter data
# loc to access the value:
df.query('LeftRight=="R"').loc['D1-0021','classification']

Update: when R coming from a variable:

var = 'R'

df.query('LeftRight==@var').loc['D1-0021','classification']
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • If the "R" come from a variable as exemple : leftOrRight = "R". How do you replace the "R" with the variable name ? – Aubrey Dekker Apr 13 '21 at 17:38
  • Thanks ! Do you know where i can reed about this ? I dont know what to type on google for that ^^' – Aubrey Dekker Apr 13 '21 at 17:42
  • 1
    @AubreyDekker the official documents on [query](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.query.html) and on [indexing](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-query) are good start. – Quang Hoang Apr 13 '21 at 17:45
1

To select rows whose column value equals a scalar, some_value, use ==:

df.loc[df['column_name'] == some_value]

in your example below should be like:

table.loc[D1-0021['classification '] == Benign]