2

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?

2
  • 1
    you can try .to_list() so in your example: df_my[df_my["id"]==i]["major"].to_list() Commented Apr 27, 2022 at 5:17
  • 1
    df_my.loc[df_my["id"]==i]["major"](where i is 3) should return Science right? Commented Apr 27, 2022 at 5:18

3 Answers 3

3

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'
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.