SOK
SOK

Reputation: 1792

Define specific cells as a variable in a pandas dataframe

Hi I have the following Dataframe

Name    Age    Height    Country
Jack    18      185       AU
Jill    16      159       NZ
Geoff   16      177       US
Tess    15      155       AU

I am trying to find a specifc row and then define two variables to use in other calculations.

For example I want to know the height and country of Geoff

name = 'Geoff' h = 177 c= US

Does anyone know the most efficient way of searching and finiding this? Thanks!

Upvotes: 2

Views: 300

Answers (2)

Pat Keenan
Pat Keenan

Reputation: 22

You can use df.loc which freezes the entire row

Example:

df.loc[df['Name'] == "Geoff"].Height

Upvotes: 0

A DUBEY
A DUBEY

Reputation: 846

Alternatively, you can also use df.query

df.query('Name == "Geoff"') # To get an entire row based on the filter condition
df.query('Name == "Geoff"').Height # For a specific column value (Height in this case) based on the filter
# or
df.query('Name == "Geoff"')['Height']

Upvotes: 2

Related Questions