Get cell value from a pandas DataFrame row

Question:

I have a simple DataFrame and I need to be able to get the value of a specific cell. Here is a sample of my DataFrame:

     iata               name        city state country
2144  M15       Perry County      Linden    TN     USA
2391  N69         Stormville  Stormville    NY     USA
861   ARA  Acadiana Regional  New Iberia    LA     USA
2596  PDX      Portland Intl    Portland    OR     USA
3238  VES       Darke County  Versailles    OH     USA

As an example of what I am trying to do, I need to get the value of the name column for the row where the iata column equals ‘PDX’. I can use airports[airports.iata == 'PDX'].name to retrieve the row I want:

2596    Portland Intl
Name: name, dtype: object

The question is now, how do I retrieve the value of the name cell for this row?

Asked By: Tony Piazza

||

Answers:

.loc returns series when you do boolean indexing because there are chances that you have multiple match cases.

df.loc[df.iata == 'PDX','name']

2596    Portland Intl
Name: name, dtype: object

If you want only the value then convert it to list and then access according to the index

df.loc[df.iata == 'PDX','name'].tolist()[0]

Or access based on the index in values i.e

df.loc[df.iata == 'PDX','name'].values[0]
Answered By: Bharath M Shetty

Alternative ways to get the scalar values of a single value Series are:

val = df.loc[df['iata'] == 'PDX', 'name'].squeeze()

val = df.loc[df['iata'] == 'PDX', 'name'].item()

val = df.loc[df['iata'] == 'PDX', 'name'].iat[0]

All of them return 'Portland Intl'.

Answered By: cottontail