question2.py 873 B

12345678910111213141516171819202122232425
  1. #!/usr/bin/env python3
  2. import numpy as np
  3. import pandas as pd
  4. from gapminder import gapminder
  5. df = gapminder.copy()
  6. N = int(input("Enter the number of countries to display: "))
  7. w = input("Enter Y to display the top {} countries based on GDP: ".format(N))
  8. if w == 'Y':
  9. meandf = df.groupby('country')['gdpPercap'].mean()
  10. sortdf = meandf.sort_values(ascending=False).head(N)
  11. resetdf = sortdf.reset_index()
  12. print("\n'''Growth Percentage of top {} countries (1952-2007) Data'''".format(N))
  13. for name in resetdf['country']:
  14. country = df.loc[df['country'] == name]
  15. country = country.set_index('year', drop = True)
  16. country.index.name = None
  17. country = country.loc[:, 'lifeExp':'gdpPercap']
  18. country.columns = ['Life Exp', 'Population', 'GDPperCapita']
  19. country = country.pct_change()*100
  20. print("\n{}'s Growth Percentage Information \n {}".format(name, country))