Aubrey Dekker
Aubrey Dekker

Reputation: 43

How to select a specific row and column

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

Upvotes: 0

Views: 80

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150735

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']

Upvotes: 2

davcli
davcli

Reputation: 121

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]

Upvotes: 1

Related Questions