Guest User

Untitled

a guest
Nov 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. def answer_one():
  2. import pandas as pd
  3. import numpy as np
  4.  
  5. x = pd.ExcelFile('Energy Indicators.xls')
  6. energy = x.parse(skiprows=17,skip_footer=(38))
  7. energy = energy[['Unnamed: 1','Petajoules','Gigajoules','%']]
  8. energy.columns = ['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']
  9. energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']] = energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']].replace('...',np.NaN).apply(pd.to_numeric)
  10. energy['Energy Supply'] = energy['Energy Supply']*1000000
  11. energy['Country'] = energy['Country'].replace({'China, Hong Kong Special Administrative Region':'Hong Kong','United Kingdom of Great Britain and Northern Ireland':'United Kingdom','Republic of Korea':'South Korea','United States of America':'United States','Iran (Islamic Republic of)':'Iran'})
  12. energy['Country'] = energy['Country'].str.replace(r" \(.*\)","")
  13.  
  14. GDP = pd.read_csv('world_bank.csv',skiprows=4)
  15. GDP['Country Name'] = GDP['Country Name'].replace('Korea, Rep.','South Korea')
  16. GDP['Country Name'] = GDP['Country Name'].replace('Iran, Islamic Rep.','Iran')
  17. GDP['Country Name'] = GDP['Country Name'].replace('Hong Kong SAR, China','Hong Kong')
  18. GDP = GDP[['Country Name','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']]
  19. GDP.columns = ['Country','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']
  20.  
  21. ScimEn = pd.read_excel(io='scimagojr-3.xlsx')
  22. ScimEn_m = ScimEn[:15]
  23.  
  24. df = pd.merge(ScimEn_m,energy,how='inner',left_on='Country',right_on='Country')
  25. final_df = pd.merge(df,GDP,how='inner',left_on='Country',right_on='Country')
  26. final_df = final_df.set_index('Country')
  27.  
  28. return final_df
  29.  
  30. answer_one()
Add Comment
Please, Sign In to add comment