Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get cell value from a pandas DataFrame row

I am new to pandas and have what I think should be a simple 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:

>>> airports.sample(5)
     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 == 'PDX'. I can use the following syntax to retrieve the row I want:

>>> airports[airports.iata == 'PDX'].name
2596    Portland Intl
Name: name, dtype: object

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

like image 590
Tony Piazza Avatar asked Dec 21 '17 03:12

Tony Piazza


1 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]
like image 88
Bharath Avatar answered Oct 11 '22 07:10

Bharath