Saturday, June 16, 2018

World Population Trend Analysis using Python Pandas and Seaborn Charts

Download population data from data.gov

populationbycountry19802010millions.csv

Contents


Ø  Read data
Ø  Cleanup data
Ø  World Population as of 2010 – Bar plot

Ø  Population Trend Analysis.
o   Point Plot
o   Box Plot
o   Bar Plot

Ø  Population Growth Rate and Percentage.
o   Growth Rate - Bar Plot
o   Growth Percent - Bar Plot


Read data.

 ## Read data  
 ##  
 import pandas as pd  
 pop=pd.read_csv("populationbycountry19802010millions.csv")  

Cleanup data.

Fill na, rename and convert data type
 pop=pop.fillna(0)  
 pop=pop.replace('--',0)  
 pop=pop.rename(columns={"Unnamed: 0": "geo_location"})  
 ##  
 for i in range(2008,2011):  
   pop[str(i)]=pop[str(i)].astype('float')  

Select top 20 geo locations based on 2010 population
 pop=pop.nlargest(10, '2010').reset_index(drop=True)  
 pop.head()  






World Population Comparison - Year 2010.

 pop=pop.nlargest(10, '2010').reset_index(drop=True)  
 %matplotlib inline  
 import seaborn as sns  
 import matplotlib.pyplot as plt  
 plt.figure( figsize = (20,6))  
 #set style of plots  
 sns.set_style('white')  
 sns.plt.xlabel('geo_location',weight='bold',size=22)  
 sns.plt.xticks(weight='bold',size=15,rotation=-45,color='blue')  
 sns.plt.yticks(weight='bold',size=15,color='blue')  
 #####################################  
 g=sns.barplot( y = '2010',  
      x = 'geo_location',  
      data = pop)  
 ##  
 g.set_title('World Population 2010',size=40)  
 g.set_ylabel('Population in Millions',size=22,weight='bold')  
 g.set_xlabel('Geographic Region',size=22,weight='bold')  












World Population Trend Analysis - Point Plot


 v=pop.columns.drop('geo_location')  
 popM=pd.melt(pop, id_vars='geo_location', value_vars=v)  
 popM['value']=popM['value'].astype('float')  
 #popM=popM[popM['geo_location']in('World')]  
 popM=popM.set_index('geo_location')  
 popM=popM.loc[['World','Asia & Oceania','Africa','China','India','United States']].reset_index()  
 popM.head()  

 plt.figure( figsize = (20,8))  
 #set style of plots  
 sns.set_style('white')  
 sns.plt.xlabel('geo_location',weight='bold',size=22)  
 sns.plt.xticks(weight='bold',size=15,rotation=-90,color='blue')  
 sns.plt.yticks(weight='bold',size=15,color='blue')  
 #####################################  
 g=sns.pointplot(x='variable', y='value', hue='geo_location',  
      data = popM)  
 ##  
 g.set_title('Population Growth Trend (1980 - 2010)',size=30)  
 g.set_ylabel('Population in Millions',size=22,weight='bold')  
 g.set_xlabel('Year',size=22,weight='bold')  
 ##  
 plt.setp(g.get_legend().get_texts(), fontsize='15') # for legend text  
 plt.setp(g.get_legend().get_title(), fontsize='18')  













World Population Trend Analysis - Box Plot


 plt.figure( figsize = (20,10))  
 #set style of plots  
 sns.set_style('white')  
 sns.plt.xlabel('geo_location',weight='bold',size=22)  
 sns.plt.xticks(weight='bold',size=20,rotation=-90,color='blue')  
 sns.plt.yticks(weight='bold',size=15,color='blue')  
 #####################################  
 g=sns.boxplot( x = popM['geo_location'],y = popM['value'])  
 ##  
 g.set_title('Population Growth Trend (1980 - 2010)',size=30)  
 g.set_ylabel('Population in Millions',size=22,weight='bold')  
 g.set_xlabel('Geographic Region',size=22,weight='bold')  
 ##  



World Population Trend Analysis - Bar Plot


 plt.figure( figsize = (20,10))  
 ##set style of plots  
 sns.set_style('white')  
 sns.plt.xlabel('geo_location',weight='bold',size=22)  
 sns.plt.xticks(weight='bold',size=20,rotation=-90,color='blue')  
 sns.plt.yticks(weight='bold',size=15,color='blue')  
 #####################################  
 g=sns.barplot(x='geo_location', y='value', hue='variable', data=popM);  
 ##  
 g.set_title('Population Growth Trend (1980 - 2010)',size=30)  
 g.set_ylabel('Population in Millions',size=22,weight='bold')  
 g.set_xlabel('Geographic Region',size=22,weight='bold')  
 ##  



World Population Growth Rate and Percent


 ##### Prepare Growth Rate and Percentage over the years  
 import numpy as np  
 popM.head()  
 popM['variable']=popM['variable'].astype('int')  
 popM_prev=popM.copy()  
 popM_prev=popM_prev.rename(columns={"value": "value_prev"})  
 popM_prev['variable']=popM_prev['variable'] +1  
 popM=popM.merge(popM_prev,on=['geo_location','variable'],how='left')  
 ##  
 popM=popM.fillna(0)  
 popM['growth']=np.where(popM['value_prev']==0,0,popM['value']-popM['value_prev'])  
 popM['growth_percent']=(popM['growth']/popM['value_prev'])*100  
 popM=popM[['geo_location','variable','value','growth','growth_percent']]  
 popD=popM[popM['variable']!=1980]  
 popD.head()  





World Population Growth Rate - Bar Plot

 plt.figure( figsize = (20,10))  
 ##set style of plots  
 sns.set_style('white')  
 sns.plt.xlabel('geo_location',weight='bold',size=22)  
 sns.plt.xticks(weight='bold',size=20,rotation=-90,color='blue')  
 sns.plt.yticks(weight='bold',size=15,color='blue')  
 #####################################  
 g=sns.barplot(x='geo_location', y='growth', hue='variable', data=popD);  
 ##  
 g.set_title('Population Growth Rate (1980 - 2010)',size=30)  
 g.set_ylabel('Population in Millions',size=22,weight='bold')  
 g.set_xlabel('Geographic Region',size=22,weight='bold')  
 ##  




World Population Growth Percent - Bar Plot


 ##set style of plots  
 plt.figure( figsize = (20,15))  
 sns.set_style('white')  
 sns.plt.xlabel('geo_location',weight='bold',size=22)  
 sns.plt.xticks(weight='bold',size=20,rotation=-90,color='blue')  
 sns.plt.yticks(weight='bold',size=15,color='blue')  
 #####################################  
 g=sns.barplot(x='geo_location', y='growth_percent', hue='variable', data=popD);  
 ##  
 g.set_title('Population Growth Rate Percent (1980 - 2010)',size=30)  
 g.set_ylabel('Population Rate Percent',size=22,weight='bold')  
 g.set_xlabel('Geographic Region',size=22,weight='bold')  
 ##  

4 comments:

  1. I liked your work and the way in which you have shared this article here about scientist It is a beneficial and helpful article for us. Thanks for sharing an article like this. Worlds first image scientist

    ReplyDelete
  2. You have now land on the true website of model Call Girls contact number in Ajmer at our expert female Call Girls contact number in Ajmer. Real mobile number Call Girls contact number in Ajmer and discover pleasant Call Girls contact number in Ajmer for yourself if you are looking for celebrity escorts in Ahmedabad.

    ReplyDelete
  3. Thank you so much for sharing very informative blog who want learn data science with python this blog will very helpful thank you so much . and who want learn about data science course in mumbai just click here

    ReplyDelete