Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract values from a Pandas DataFrame, rather than a Series (without referencing the index)?

I am trying to return a specific item from a Pandas DataFrame via conditional selection (and do not want to have to reference the index to do so).

Here is an example:

I have the following dataframe:

  Code  Colour  Fruit
0   1   red     apple
1   2   orange  orange
2   3   yellow  banana
3   4   green   pear
4   5   blue    blueberry

I enter the following code to search for the code for blueberries:

df[df['Fruit'] == 'blueberry']['Code']

This returns:

4    5
Name: Code, dtype: int64

which is of type:

pandas.core.series.Series

but what I actually want to return is the number 5 of type:

numpy.int64

which I can do if I enter the following code:

df[df['Fruit'] == 'blueberry']['Code'][4]

i.e. referencing the index to give the number 5, but I do not want to have to reference the index!

Is there another syntax that I can deploy here to achieve the same thing?

Thank you!...

Update:

One further idea is this code:

df[df['Fruit'] == 'blueberry']['Code'][df[df['Fruit']=='blueberry'].index[0]]

However, this does not seem particularly elegant (and it references the index). Is there a more concise and precise method that does not need to reference the index or is this strictly necessary?

Thanks!...

like image 452
agftrading Avatar asked Mar 14 '18 12:03

agftrading


1 Answers

Let's try this:

df.loc[df['Fruit'] == 'blueberry','Code'].values[0]

Output:

5

First, use .loc to access the values in your dataframe using the boolean indexing for row selection and index label for column selection. The convert that returned series to an array of values and since there is only one value in that array you can use index '[0]' get the scalar value from that single element array.

like image 188
Scott Boston Avatar answered Sep 20 '22 16:09

Scott Boston