-1

As an example, if I were to have a DataFrame that looked like the following:

 CONTINENT     COUNTRY    POPULATION
    Europe      France         67.06
    Europe       Italy         60.36
    Europe     Denmark          5.80
      Asia       Japan        126.30
      Asia       China       1398.00
N. America      Canada         37.59
    Europe    Portugal         10.28
      Asia    S. Korea         51.71

How would I go about selecting the POPULATION figures for all the countries in Europe?

I tried a simple df.loc['Europe', 'POPULATION'] but that didn't turn out correctly. Probably quite a simple solution but I can't even find the right words/phrase to Google it properly!

mlan
  • 119
  • 1
  • 9

2 Answers2

1

When you use df.loc the first item refers to the index. You want the values of a column where other column has a certain value. You should find the indices of the places where this condition is True and then use df.loc. Try the following:

df.loc[(df['CONTINENT'] == 'Europe'), 'POPULATION']
  • That's perfect, thank you. Is there a way to keep the country names displayed in the resulting DataFrame? – mlan Jun 22 '21 at 14:46
  • 1
    Add `df.set_index(keys='COUNTRY', inplace=True)` before Diego's line @mlan. And you will be good to tick his answer. – keepAlive Jun 22 '21 at 14:56
  • 1
    Yes! Instead of selecting a single column you could select a list of columns. In your case you should do: `df.loc[(df['CONTINENT'] == 'Europe'), ['POPULATION','COUNTRY']]` – Diego Pereyra Jun 22 '21 at 14:58
  • 1
    Great - thanks both of you! – mlan Jun 22 '21 at 14:59
1

Assuming data is in df, you could also try this:

df[ df['CONTINENT'] == 'Europe' ]['POPULATION']
Hang Yan
  • 161
  • 1
  • 5