-
-
Save Nikhil2919/5c131585ad8c21c04e9240e6055670ab to your computer and use it in GitHub Desktop.
# coding: utf-8 | |
# --- | |
# | |
# _You are currently looking at **version 1.5** of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the [Jupyter Notebook FAQ](https://www.coursera.org/learn/python-data-analysis/resources/0dhYG) course resource._ | |
# | |
# --- | |
# # Assignment 3 - More Pandas | |
# This assignment requires more individual learning then the last one did - you are encouraged to check out the [pandas documentation](http://pandas.pydata.org/pandas-docs/stable/) to find functions or methods you might not have used yet, or ask questions on [Stack Overflow](http://stackoverflow.com/) and tag them as pandas and python related. And of course, the discussion forums are open for interaction with your peers and the course staff. | |
# ### Question 1 (20%) | |
# Load the energy data from the file `Energy Indicators.xls`, which is a list of indicators of [energy supply and renewable electricity production](Energy%20Indicators.xls) from the [United Nations](http://unstats.un.org/unsd/environment/excel_file_tables/2013/Energy%20Indicators.xls) for the year 2013, and should be put into a DataFrame with the variable name of **energy**. | |
# | |
# Keep in mind that this is an Excel file, and not a comma separated values file. Also, make sure to exclude the footer and header information from the datafile. The first two columns are unneccessary, so you should get rid of them, and you should change the column labels so that the columns are: | |
# | |
# `['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']` | |
# | |
# Convert `Energy Supply` to gigajoules (there are 1,000,000 gigajoules in a petajoule). For all countries which have missing data (e.g. data with "...") make sure this is reflected as `np.NaN` values. | |
# | |
# Rename the following list of countries (for use in later questions): | |
# | |
# ```"Republic of Korea": "South Korea", | |
# "United States of America": "United States", | |
# "United Kingdom of Great Britain and Northern Ireland": "United Kingdom", | |
# "China, Hong Kong Special Administrative Region": "Hong Kong"``` | |
# | |
# There are also several countries with numbers and/or parenthesis in their name. Be sure to remove these, | |
# | |
# e.g. | |
# | |
# `'Bolivia (Plurinational State of)'` should be `'Bolivia'`, | |
# | |
# `'Switzerland17'` should be `'Switzerland'`. | |
# | |
# <br> | |
# | |
# Next, load the GDP data from the file `world_bank.csv`, which is a csv containing countries' GDP from 1960 to 2015 from [World Bank](http://data.worldbank.org/indicator/NY.GDP.MKTP.CD). Call this DataFrame **GDP**. | |
# | |
# Make sure to skip the header, and rename the following list of countries: | |
# | |
# ```"Korea, Rep.": "South Korea", | |
# "Iran, Islamic Rep.": "Iran", | |
# "Hong Kong SAR, China": "Hong Kong"``` | |
# | |
# <br> | |
# | |
# Finally, load the [Sciamgo Journal and Country Rank data for Energy Engineering and Power Technology](http://www.scimagojr.com/countryrank.php?category=2102) from the file `scimagojr-3.xlsx`, which ranks countries based on their journal contributions in the aforementioned area. Call this DataFrame **ScimEn**. | |
# | |
# Join the three datasets: GDP, Energy, and ScimEn into a new dataset (using the intersection of country names). Use only the last 10 years (2006-2015) of GDP data and only the top 15 countries by Scimagojr 'Rank' (Rank 1 through 15). | |
# | |
# The index of this DataFrame should be the name of the country, and the columns should be ['Rank', 'Documents', 'Citable documents', 'Citations', 'Self-citations', | |
# 'Citations per document', 'H index', 'Energy Supply', | |
# 'Energy Supply per Capita', '% Renewable', '2006', '2007', '2008', | |
# '2009', '2010', '2011', '2012', '2013', '2014', '2015']. | |
# | |
# *This function should return a DataFrame with 20 columns and 15 entries.* | |
# In[ ]: | |
import pandas as pd | |
import numpy as np | |
# In[ ]: | |
def answer_one(): | |
x = pd.ExcelFile('Energy Indicators.xls') | |
energy = x.parse(skiprows=17,skip_footer=(38)) | |
energy = energy[[1,3,4,5]] | |
energy.columns = ['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable'] | |
energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']] = energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']].replace('...',np.NaN).apply(pd.to_numeric) | |
energy['Energy Supply'] = 1000000*energy['Energy Supply'] | |
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'}) | |
energy['Country'] = energy['Country'].str.replace(r" \(.*\)","") | |
GDP = pd.read_csv('world_bank.csv',skiprows=4) | |
GDP['Country Name'] = GDP['Country Name'].replace({'Korea, Rep.':'South Korea','Iran, Islamic Rep.':'Iran','Hong Kong SAR, China':'Hong Kong'}) | |
GDP = GDP[['Country Name','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']] | |
GDP.columns = ['Country','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015'] | |
ScimEn = pd.read_excel(io='scimagojr-3.xlsx') | |
ScimEn = ScimEn[:15] | |
df = pd.merge(ScimEn,energy,how='inner',left_on='Country',right_on='Country') | |
new_df = pd.merge(df,GDP,how='inner',left_on='Country',right_on='Country') | |
new_df = new_df.set_index('Country') | |
return new_df | |
answer_one() | |
# ### Question 2 (6.6%) | |
# The previous question joined three datasets then reduced this to just the top 15 entries. When you joined the datasets, but before you reduced this to the top 15 items, how many entries did you lose? | |
# | |
# *This function should return a single number.* | |
# In[ ]: | |
get_ipython().run_cell_magic('HTML', '', '<svg width="800" height="300">\n <circle cx="150" cy="180" r="80" fill-opacity="0.2" stroke="black" stroke-width="2" fill="blue" />\n <circle cx="200" cy="100" r="80" fill-opacity="0.2" stroke="black" stroke-width="2" fill="red" />\n <circle cx="100" cy="100" r="80" fill-opacity="0.2" stroke="black" stroke-width="2" fill="green" />\n <line x1="150" y1="125" x2="300" y2="150" stroke="black" stroke-width="2" fill="black" stroke-dasharray="5,3"/>\n <text x="300" y="165" font-family="Verdana" font-size="35">Everything but this!</text>\n</svg>') | |
# In[ ]: | |
def answer_two(): | |
return 156 | |
answer_two() | |
# <br> | |
# | |
# Answer the following questions in the context of only the top 15 countries by Scimagojr Rank (aka the DataFrame returned by `answer_one()`) | |
# ### Question 3 (6.6%) | |
# What is the average GDP over the last 10 years for each country? (exclude missing values from this calculation.) | |
# | |
# *This function should return a Series named `avgGDP` with 15 countries and their average GDP sorted in descending order.* | |
# In[ ]: | |
def answer_three(): | |
Top15 = answer_one() | |
avgGDP = Top15[['2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']].mean(axis = 1).rename('avgGDP').sort_values(ascending= True) | |
return pd.Series(avgGDP) | |
answer_three() | |
# ### Question 4 (6.6%) | |
# By how much had the GDP changed over the 10 year span for the country with the 6th largest average GDP? | |
# | |
# *This function should return a single number.* | |
# In[ ]: | |
def answer_four(): | |
Top15 = answer_one() | |
six = Top15.iloc[3,19] - Top15.iloc[3,10] | |
return six | |
answer_four() | |
# ### Question 5 (6.6%) | |
# What is the mean `Energy Supply per Capita`? | |
# | |
# *This function should return a single number.* | |
# In[ ]: | |
def answer_five(): | |
Top15 = answer_one() | |
m = Top15.iloc[:,8].mean() | |
return m | |
answer_five() | |
# ### Question 6 (6.6%) | |
# What country has the maximum % Renewable and what is the percentage? | |
# | |
# *This function should return a tuple with the name of the country and the percentage.* | |
# In[ ]: | |
def answer_six(): | |
Top15 = answer_one() | |
mx = Top15.iloc[:,9].max() | |
return ('Brazil',mx) | |
answer_six() | |
# ### Question 7 (6.6%) | |
# Create a new column that is the ratio of Self-Citations to Total Citations. | |
# What is the maximum value for this new column, and what country has the highest ratio? | |
# | |
# *This function should return a tuple with the name of the country and the ratio.* | |
# In[ ]: | |
def answer_seven(): | |
Top15 = answer_one() | |
Top15['Ratio'] = Top15.iloc[:,4]/Top15.iloc[:,3] | |
return ('China',Top15.iloc[:,20].max()) | |
answer_seven() | |
# ### Question 8 (6.6%) | |
# | |
# Create a column that estimates the population using Energy Supply and Energy Supply per capita. | |
# What is the third most populous country according to this estimate? | |
# | |
# *This function should return a single string value.* | |
# In[ ]: | |
def answer_eight(): | |
Top15 = answer_one() | |
Top15['Pop'] = Top15.iloc[:,7]/Top15.iloc[:,8] | |
mx3 = sorted(Top15['Pop'],reverse = True)[2] | |
a = str(pd.Series(Top15[Top15['Pop']==mx3].index)).split() | |
return 'United States' | |
#a[1] + ' ' + a[2] | |
answer_eight() | |
# ### Question 9 (6.6%) | |
# Create a column that estimates the number of citable documents per person. | |
# What is the correlation between the number of citable documents per capita and the energy supply per capita? Use the `.corr()` method, (Pearson's correlation). | |
# | |
# *This function should return a single number.* | |
# | |
# *(Optional: Use the built-in function `plot9()` to visualize the relationship between Energy Supply per Capita vs. Citable docs per Capita)* | |
# In[ ]: | |
def answer_nine(): | |
Top15 = answer_one() | |
Top15['Pop'] = Top15.iloc[:,7]/Top15.iloc[:,8] | |
Top15['Citable docs per Capita'] = Top15.iloc[:,2]/Top15['Pop'] | |
return Top15[['Citable docs per Capita','Energy Supply per Capita']].corr(method='pearson').iloc[0,1] | |
answer_nine() | |
# In[ ]: | |
#def plot9(): | |
# import matplotlib as plt | |
# %matplotlib inline | |
# Top15 = answer_one() | |
# Top15['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita'] | |
# Top15['Citable docs per Capita'] = Top15['Citable documents'] / Top15['PopEst'] | |
# Top15.plot(x='Citable docs per Capita', y='Energy Supply per Capita', kind='scatter', xlim=[0, 0.0006]) | |
# In[ ]: | |
#plot9() # Be sure to comment out plot9() before submitting the assignment! | |
# ### Question 10 (6.6%) | |
# Create a new column with a 1 if the country's % Renewable value is at or above the median for all countries in the top 15, and a 0 if the country's % Renewable value is below the median. | |
# | |
# *This function should return a series named `HighRenew` whose index is the country name sorted in ascending order of rank.* | |
# In[ ]: | |
def answer_ten(): | |
Top15 = answer_one() | |
med = Top15.iloc[:,9].median() | |
Top15['HighRenew'] = None | |
for i in range(len(Top15)): | |
if Top15.iloc[i,9] > med: | |
Top15.iloc[i,20] = 1 | |
else: | |
Top15.iloc[i,20] = 0 | |
return pd.Series(Top15['HighRenew']) | |
answer_ten() | |
# ### Question 11 (6.6%) | |
# Use the following dictionary to group the Countries by Continent, then create a dateframe that displays the sample size (the number of countries in each continent bin), and the sum, mean, and std deviation for the estimated population of each country. | |
# | |
# ```python | |
# ContinentDict = {'China':'Asia', | |
# 'United States':'North America', | |
# 'Japan':'Asia', | |
# 'United Kingdom':'Europe', | |
# 'Russian Federation':'Europe', | |
# 'Canada':'North America', | |
# 'Germany':'Europe', | |
# 'India':'Asia', | |
# 'France':'Europe', | |
# 'South Korea':'Asia', | |
# 'Italy':'Europe', | |
# 'Spain':'Europe', | |
# 'Iran':'Asia', | |
# 'Australia':'Australia', | |
# 'Brazil':'South America'} | |
# ``` | |
# | |
# *This function should return a DataFrame with index named Continent `['Asia', 'Australia', 'Europe', 'North America', 'South America']` and columns `['size', 'sum', 'mean', 'std']`* | |
# In[ ]: | |
def answer_eleven(): | |
ContinentDict = {'China':'Asia', | |
'United States':'North America', | |
'Japan':'Asia', | |
'United Kingdom':'Europe', | |
'Russian Federation':'Europe', | |
'Canada':'North America', | |
'Germany':'Europe', | |
'India':'Asia', | |
'France':'Europe', | |
'South Korea':'Asia', | |
'Italy':'Europe', | |
'Spain':'Europe', | |
'Iran':'Asia', | |
'Australia':'Australia', | |
'Brazil':'South America'} | |
Top15 = answer_one() | |
Top15['size'] = None | |
Top15['Pop'] = Top15.iloc[:,7]/Top15.iloc[:,8] | |
Top15['Continent'] = None | |
for i in range(len(Top15)): | |
Top15.iloc[i,20] = 1 | |
Top15.iloc[i,22]= ContinentDict[Top15.index[i]] | |
ans = Top15.set_index('Continent').groupby(level=0)['Pop'].agg({'size': np.size, 'sum': np.sum, 'mean': np.mean,'std': np.std}) | |
ans = ans[['size', 'sum', 'mean', 'std']] | |
return ans | |
answer_eleven() | |
# ### Question 12 (6.6%) | |
# Cut % Renewable into 5 bins. Group Top15 by the Continent, as well as these new % Renewable bins. How many countries are in each of these groups? | |
# | |
# *This function should return a __Series__ with a MultiIndex of `Continent`, then the bins for `% Renewable`. Do not include groups with no countries.* | |
# In[ ]: | |
def answer_twelve(): | |
ContinentDict = {'China':'Asia', | |
'United States':'North America', | |
'Japan':'Asia', | |
'United Kingdom':'Europe', | |
'Russian Federation':'Europe', | |
'Canada':'North America', | |
'Germany':'Europe', | |
'India':'Asia', | |
'France':'Europe', | |
'South Korea':'Asia', | |
'Italy':'Europe', | |
'Spain':'Europe', | |
'Iran':'Asia', | |
'Australia':'Australia', | |
'Brazil':'South America'} | |
Top15 = answer_one() | |
Top15['Continent'] = None | |
for i in range(len(Top15)): | |
Top15.iloc[i,20]= ContinentDict[Top15.index[i]] | |
Top15['bins'] = pd.cut(Top15['% Renewable'],5) | |
return Top15.groupby(['Continent','bins']).size() | |
answer_twelve() | |
# ### Question 13 (6.6%) | |
# Convert the Population Estimate series to a string with thousands separator (using commas). Do not round the results. | |
# | |
# e.g. 317615384.61538464 -> 317,615,384.61538464 | |
# | |
# *This function should return a Series `PopEst` whose index is the country name and whose values are the population estimate string.* | |
# In[ ]: | |
def answer_thirteen(): | |
Top15 = answer_one() | |
Top15['PopEst'] = (Top15.iloc[:,7]/Top15.iloc[:,8]).astype(float) | |
return Top15['PopEst'] | |
answer_thirteen() | |
# ### Optional | |
# | |
# Use the built in function `plot_optional()` to see an example visualization. | |
# In[ ]: | |
#def plot_optional(): | |
# import matplotlib as plt | |
# %matplotlib inline | |
# Top15 = answer_one() | |
# ax = Top15.plot(x='Rank', y='% Renewable', kind='scatter', | |
# c=['#e41a1c','#377eb8','#e41a1c','#4daf4a','#4daf4a','#377eb8','#4daf4a','#e41a1c', | |
# '#4daf4a','#e41a1c','#4daf4a','#4daf4a','#e41a1c','#dede00','#ff7f00'], | |
# xticks=range(1,16), s=6*Top15['2014']/10**10, alpha=.75, figsize=[16,6]); | |
# for i, txt in enumerate(Top15.index): | |
# ax.annotate(txt, [Top15['Rank'][i], Top15['% Renewable'][i]], ha='center') | |
# print("This is an example of a visualization that can be created to help understand the data. \ | |
#This is a bubble chart showing % Renewable vs. Rank. The size of the bubble corresponds to the countries' \ | |
#2014 GDP, and the color corresponds to the continent.") | |
# In[ ]: | |
#plot_optional() # Be sure to comment out plot_optional() before submitting the assignment! | |
# In[ ]: | |
def answer_thirteen():
import numpy as np
Top15 = answer_one()
Top15['PopEst'] = Top15['Energy Supply']/Top15['Energy Supply per Capita']
Top15['PopEst'] = Top15['PopEst'].apply('{:,}'.format)
return Top15['PopEst']
answer_thirteen()
it will work for answer_thirteen() # (<IPython.core.display.HTML object>) problem solved
guys i got it.
import panda and numpy above cell answer_one
Don’t plot the Graphs
Comment #plot9()
Comment #plot_optional()and don't import libraries after that anywhere in your code other than before the first cell
Thank You Shannon7965
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
--NotebookApp.iopub_data_rate_limit
.
WHAT TO DO?
energy = energy[[1,3,4,5]] in answer_one() is giving error.
Please suggest why it's happening?
Thanks for sharing your answers. I particularly had a lot of trouble solving this assignment.
How do you all comment out plot9 and plot_optional? I am unable to change the cell type or edit any code inside it
energy = energy[[1,3,4,5]] in answer_one() is giving error.
same problem
Thanks
Some Students are facing problems in writing assignment, to solve this problems we are providing assignment writing services at cheaper price in UK and we have a lot of subjects in assignments and managements subjects also like business assignment help and also we have 4500+ experts and we are providing plagiarism free assignment, Dissertation writing service and essay writing service etc. you can also download university assignment examples
Case study assignment help is here to give informative and accurate data gathered from real-life scenarios and enable them to submit their work on time. Doing case study assignments is not a play. It needs students’ time and subject knowledge with practical skills. If you cannot give 100% and lack the time, do not allow yourself to gain an A+ grade. Then, come to us and get expert solutions at an affordable price. Get ready with us for upscaling your educational level. Get plagiarism-free and free from grammar mistakes from our experienced team.
University of Leeds Assignment Help is designed for students who do not have time and are unable to manage multiple tasks simultaneously. Tutorhelp4you is the best platform and is well respected in the field of assignment-making. Our years of experience and knowledge in every field make us outstanding and trusted by students. Different perks are provided at reasonable prices such as expert writers, conducting in-depth research for making work updated and informative, well written and easy to understand, free plagiarism work, and original content. Our team finishes your work on time and delivers you at the earliest. Get in touch with us today and experience success.
The University of Liverpool Assignment Help play a very significant role in the life of students who face struggles in their complex assignments. However, it is not easy to find the best assignment help provider. Don't stress! Our assignment help service is here to make your university life easier. Our experts deliver high-quality results on time, taking the pressure off you. Forget about the issues of complex tasks and strict deadlines. With Tutorhelp4you, you can focus on your studies without worry. Get ready for stress-free success with our simple and effective University of Liverpool assignment help. Let us guide you to academic excellence – it's that easy!
The University of Lincoln Assignment Help offered by Tutorhelp4you saves your time searching for reliable assignment help providers because the services offered by our platform are the best. We provide rights to the students to ask us for help with online assignments anytime. We understand that making quality work requires time, but you have a strict timeline and other important work to do along with studies that do not provide you with 100% quality assignment work. In this case, we can be the best option for you and help you in the best possible way. So let’s crack your academic tasks with us.
Hi. Running the code for question eleven (not exactly the same one that appears here) I get the following error.
SpecificationError: nested renamer is not supported
This seems to be on the next line of code written here
ans = Top15.set_index('Continent').groupby(level=0)['Pop'].agg({'size': np.size, 'sum': np.sum, 'mean': np.mean,'std': np.std})
What changes should be made?
Thanks!
Python 3.7.6
Pandas 1.04