asmgx
asmgx

Reputation: 7984

how to get a single value from dataframe only in Python

I have dataframe df_my that looks like this

      id    name        age     major
----------------------------------------
0     1     Mark        34      English
1     2     Tom         55      Art
2     3     Peter       31      Science
3     4     Mohammad    23      Math
4     5     Mike        47      Art
...

I am trying to get the value of major (only)

I used this and it works fine when I know the id of the record

df_my["major"][3]

returns

"Math"

great

but I want to get the major for a variable record

I used

i = 3
df_my.loc[df_my["id"]==i]["major"]

and also used

i = 3
df_my[df_my["id"]==i]["major"]

but they both return

3     Math

it includes the record index too

how can I get the major only and nothing else?

Upvotes: 1

Views: 7640

Answers (3)

Arne
Arne

Reputation: 10545

If one of the columns of your dataframe is the natural primary index for those data, then it's usually a good idea to make pandas aware of it by setting the index accordingly:

df_my.set_index('id', inplace=True)

Now you can easily get just the major value for any id value i:

df_my.loc[i, 'major']

Note that for i = 3, the output is 'Science', which is expected, as noted in the comments to your question above.

Upvotes: 0

Irgendniemand
Irgendniemand

Reputation: 236

I also stumbled over this problem, from a little different angle:

df = pd.DataFrame({'First Name': ['Kumar'],
                   'Last Name': ['Ram'],
                   'Country': ['India'],
                   'num_var': 1})

>>> df.loc[(df['First Name'] == 'Kumar'), "num_var"]
0    1
Name: num_var, dtype: int64

>>> type(df.loc[(df['First Name'] == 'Kumar'), "num_var"])
<class 'pandas.core.series.Series'>

So it returns a Series (although it is only a series with only 1 element). If you access through the index, you receive the integer.

df.loc[0, "num_var"]
1

type(df.loc[0, "num_var"])
<class 'numpy.int64'>

The answer on how to select the respective, single value was already given above. However, I think it is interesting to note that accessing through an index always gives the single value whereas accessing through a condition returns a series. This is, b/c accessing with index clearly returns only one value whereas accessing through a condition can return several values.

Upvotes: 0

user7864386
user7864386

Reputation:

You could use squeeze:

i = 3
out = df.loc[df['id']==i,'major'].squeeze()

Another option is iat:

out = df.loc[df['id']==i,'major'].iat[0]

Output:

'Science'

Upvotes: 3

Related Questions