How to get a value from a Pandas DataFrame and not the index and object type

PythonPandasDataframe

Python Problem Overview


Say I have the following DataFrame

Letter    Number
A          1
B          2
C          3
D          4

Which can be obtained through the following code

import pandas as pd

letters=pd.Series(('A', 'B', 'C', 'D'))
numbers=pd.Series((1, 2, 3, 4))
keys=('Letters', 'Numbers')
df=pd.concat((letters, numbers), axis=1, keys=keys)

Now I want to get the value C from the column Letters.

The command line

df[df.Letters=='C'].Letters

will return

2    C
Name: Letters, dtype: object

How can I get only the value C and not the whole two line output?

Python Solutions


Solution 1 - Python

df[df.Letters=='C'].Letters.item()

This returns the first element in the Index/Series returned from that selection. In this case, the value is always the first element.

EDIT:

Or you can run a loc() and access the first element that way. This was shorter and is the way I have implemented it in the past.

Solution 2 - Python

Use the values attribute to return the values as a np array and then use [0] to get the first value:

In [4]:
df.loc[df.Letters=='C','Letters'].values[0]

Out[4]:
'C'

EDIT

I personally prefer to access the columns using subscript operators:

df.loc[df['Letters'] == 'C', 'Letters'].values[0]

This avoids issues where the column names can have spaces or dashes - which mean that accessing using ..

Solution 3 - Python

import pandas as pd

dataset = pd.read_csv("data.csv")
values = list(x for x in dataset["column name"])

>>> values[0]
'item_0'

edit:

actually, you can just index the dataset like any old array.

import pandas as pd

dataset = pd.read_csv("data.csv")
first_value = dataset["column name"][0]

>>> print(first_value)
'item_0'

Solution 4 - Python

You can use loc with the index and column labels.

df.loc[2, 'Letters']
# 'C'

If you prefer the "Numbers" column as reference, you can set it as index.

df.set_index('Numbers').loc[3, 'Letters']

I find this cleaner as it does not need the [0] or .item().

Solution 5 - Python

I think a good option is to turn your single line DataFrame into a Series first, then index that:

df[df.Letters=='C'].squeeze()['Letters']

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEduardoView Question on Stackoverflow
Solution 1 - Pythonvalkn0tView Answer on Stackoverflow
Solution 2 - PythonEdChumView Answer on Stackoverflow
Solution 3 - PythonLewisView Answer on Stackoverflow
Solution 4 - PythonnocibambiView Answer on Stackoverflow
Solution 5 - PythonHarry JonesView Answer on Stackoverflow