Optional Question - Create a dataframe containing 10 countries with the lowest GDP per capita, among the counties with population greater than 100 million

Has any one tried this question?

(df[df[“population”] > 100]).sort_values(“GDPperCapita”,ascending=True).head(10)

let me know if it worked or not. And note that I haven’t used the exact column name and the value for population, you should change it before executing it.

it works thanks. Can you please break it down and explain. I am new to this & find it very difficult. thanks in advance.

It is quite simple, here, I am separating the single line code into multiple line segments. Note that It would be easy to understand if you execute each line separately and study the output by yourself.

  1. x = df[“population”] > 100 #returns list of booleans values, where True represent particular item of the population column is > 100 (means the condition is satisfied).

  2. y = df[x] #when you pass the list of booleans values as the data frame index, only the row corresponding to the index of True is considered; till now, we have filtered the data frame and considered only those rows where the population is greater than 100

  3. z = y.sort_values(“GDPperCapita”,ascending=True) # we are sorting the filtered df in ascending order with respect to GDPperCapita

  4. z.head(10) # this will return the first top 10 sorted rows of the filtered data frame.

1 Like

thanks a lot. really appreciate it.

gdp_10m_df=countries_df[countries_df[‘population’]>100e6].sort_values(by=‘gdp_per_capita’).head(10)

check this