Skip to content
Advertisement

Using .loc accessor

  sex  age  pets
0   M   21  [cat, dog]
1   M   45  [hamster]
2   F   23  [cat, gerbil]
3   M   56  [fish, hamster, gerbil]
4   F   47  [cat]
5   F   70  [dog]
6   F   34  [dog]
7   M   30  [cat]
8   F   19  [rabbit, cat]
9   M   62  [dog]

I’m trying to use .loc to find out sex of the youngest person here in the chart.

The following did not work:

pd.DataFrame.loc['age', 'sex']

Advertisement

Answer

You can achieve it like this:

>>> print(df.loc[df.age == df.age.min(), 'sex'].values[0])
'F'
Advertisement