Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ycchen00
GitHub Repository: ycchen00/Introduction-to-Data-Science-in-Python
Path: blob/main/assignments/assignment3/assignment3.ipynb
3223 views
Kernel: Python 3

Assignment 3

All questions are weighted the same in this assignment. This assignment requires more individual learning then the last one did - you are encouraged to check out the pandas documentation to find functions or methods you might not have used yet, or ask questions on Stack Overflow and tag them as pandas and python related. All questions are worth the same number of points except question 1 which is worth 17% of the assignment grade.

Note: Questions 2-13 rely on your question 1 answer.

import pandas as pd import numpy as np # Filter all warnings. If you would like to see the warnings, please comment the two lines below. import warnings warnings.filterwarnings('ignore')

Question 1

Load the energy data from the file assets/Energy Indicators.xls, which is a list of indicators of energy supply and renewable electricity production from the United Nations 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 (Note: 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):

"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 parenthesis in their name. Be sure to remove these, e.g. `'Bolivia (Plurinational State of)'` should be `'Bolivia'`. Next, load the GDP data from the file `assets/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"``` 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 `assets/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, and the rows of the DataFrame should be sorted by "Rank".*

test code

# !pip install xlrd
Energy = pd.read_excel('assets/Energy Indicators.xls',na_values=["..."],header = None,skiprows=18,skipfooter= 38,usecols=[2,3,4,5],names=['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']) # Energy # Energy = pd.read_excel("assets/Energy Indicators.xls", names=['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable'],header=17, usecols='C:F').dropna(axis=0,how='any').set_index('Country') # Energy.columns=['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable'] # Energy.replace( '...',np.nan, inplace=True) Energy['Energy Supply'] = Energy['Energy Supply'].apply(lambda x: x*1000000) # rename is also Ok but require set_index('Country') Energy['Country'] = Energy['Country'].str.replace(r" \(.*\)","") Energy['Country'] = Energy['Country'].str.replace(r"\d*","") Energy['Country'] = Energy['Country'].replace({'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'}) # Energy['Country']=Energy['Country'].replace({'Republic of Korea':'South Korea'}) # Energy['Country']=Energy['Country'].replace({'United States of America':'United States'}) # Energy['Country']=Energy['Country'].replace({'United Kingdom of Great Britain and Northern Ireland':'United Kingdom'}) # Energy['Country']=Energy['Country'].replace({'China, Hong Kong Special Administrative Region':'Hong Kong'}) # for i in Energy['Country']: # print(i) # Energy.set_index('Country').loc['Hong Kong'] # Energy.set_index('Country').loc['United States of America'] Energy.set_index('Country').loc['United States'] # Energy.set_index('Country').loc['South Korea'] # for i in Energy['Country']: # print(i) # Energy.set_index('Country').loc['Bolivia'] # Energy.set_index('Country').loc['Switzerland17'] # Energy.set_index('Country').loc['Switzerland']
Energy Supply 9.083800e+10 Energy Supply per Capita 2.860000e+02 % Renewable 1.157098e+01 Name: United States, dtype: float64
for i in Energy['Country'].replace({'United States of America' : 'United States', 'United Kingdom of Great Britain and Northern Ireland':'United Kingdom', 'China, Hong Kong Special Administrative Region':'Hong Kong'}): print(i)
Afghanistan Albania Algeria American Samoa Andorra Angola Anguilla Antigua and Barbuda Argentina Armenia Aruba Australia Austria Azerbaijan Bahamas Bahrain Bangladesh Barbados Belarus Belgium Belize Benin Bermuda Bhutan Bolivia Bonaire, Sint Eustatius and Saba Bosnia and Herzegovina Botswana Brazil British Virgin Islands Brunei Darussalam Bulgaria Burkina Faso Burundi Cabo Verde Cambodia Cameroon Canada Cayman Islands Central African Republic Chad Chile China Hong Kong China, Macao Special Administrative Region Colombia Comoros Congo Cook Islands Costa Rica Côte d'Ivoire Croatia Cuba Curaçao Cyprus Czech Republic Democratic People's Republic of Korea Democratic Republic of the Congo Denmark Djibouti Dominica Dominican Republic Ecuador Egypt El Salvador Equatorial Guinea Eritrea Estonia Ethiopia Faeroe Islands Falkland Islands Fiji Finland France French Guiana French Polynesia Gabon Gambia Georgia Germany Ghana Gibraltar Greece Greenland Grenada Guadeloupe Guam Guatemala Guernsey Guinea Guinea-Bissau Guyana Haiti Honduras Hungary Iceland India Indonesia Iran Iraq Ireland Isle of Man Israel Italy Jamaica Japan Jersey Jordan Kazakhstan Kenya Kiribati Kuwait Kyrgyzstan Lao People's Democratic Republic Latvia Lebanon Lesotho Liberia Libya Liechtenstein Lithuania Luxembourg Madagascar Malawi Malaysia Maldives Mali Malta Marshall Islands Martinique Mauritania Mauritius Mexico Micronesia Mongolia Montenegro Montserrat Morocco Mozambique Myanmar Namibia Nauru Nepal Netherlands New Caledonia New Zealand Nicaragua Niger Nigeria Niue Northern Mariana Islands Norway Oman Pakistan Palau Panama Papua New Guinea Paraguay Peru Philippines Poland Portugal Puerto Rico Qatar South Korea Republic of Moldova Réunion Romania Russian Federation Rwanda Saint Helena Saint Kitts and Nevis Saint Lucia Saint Pierre and Miquelon Saint Vincent and the Grenadines Samoa Sao Tome and Principe Saudi Arabia Senegal Serbia Seychelles Sierra Leone Singapore Sint Maarten Slovakia Slovenia Solomon Islands Somalia South Africa South Sudan Spain Sri Lanka State of Palestine Sudan Suriname Swaziland Sweden Switzerland Syrian Arab Republic Tajikistan Thailand The former Yugoslav Republic of Macedonia Timor-Leste Togo Tonga Trinidad and Tobago Tunisia Turkey Turkmenistan Turks and Caicos Islands Tuvalu Uganda Ukraine United Arab Emirates United Kingdom United Republic of Tanzania United States United States Virgin Islands Uruguay Uzbekistan Vanuatu Venezuela Viet Nam Wallis and Futuna Islands Yemen Zambia Zimbabwe
GDP = pd.read_csv('assets/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.head() # GDP.set_index('Country Name').loc['South Korea'] # GDP.set_index('Country Name').loc['Hong Kong']
ScimEn = pd.read_excel('assets/scimagojr-3.xlsx') # ScimEn.head()
merge1 = pd.merge(ScimEn,Energy,how="inner",left_on="Country",right_on="Country") merge1 = merge1[merge1["Rank"]<=15] GDP.rename(columns = {"Country Name":"Country"},inplace=True) GDP = GDP.loc[:,['2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015',"Country"]] merge2 = pd.merge(merge1,GDP,how="inner",left_on="Country",right_on="Country").set_index("Country") merge1

answer

def answer_one(): # YOUR CODE HERE # raise NotImplementedError() Energy = pd.read_excel('assets/Energy Indicators.xls',na_values=["..."],header = None,skiprows=18,skipfooter= 38,usecols=[2,3,4,5],names=['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']) Energy['Energy Supply'] = Energy['Energy Supply'].apply(lambda x: x*1000000) Energy['Country'] = Energy['Country'].str.replace(r" \(.*\)","") Energy['Country'] = Energy['Country'].str.replace(r"\d*","") Energy['Country'] = Energy['Country'].replace({'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'}) GDP = pd.read_csv('assets/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'}) ScimEn = pd.read_excel('assets/scimagojr-3.xlsx') merge1 = pd.merge(ScimEn,Energy,how="inner",left_on="Country",right_on="Country") merge1 = merge1[merge1["Rank"]<=15] GDP.rename(columns = {"Country Name":"Country"},inplace=True) GDP = GDP.loc[:,['2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015',"Country"]] merge2 = pd.merge(merge1,GDP,how="inner",left_on="Country",right_on="Country").set_index("Country") return merge2
assert type(answer_one()) == pd.DataFrame, "Q1: You should return a DataFrame!" assert answer_one().shape == (15,20), "Q1: Your DataFrame should have 20 columns and 15 entries!"
answer_one()
# Cell for autograder.

Question 2

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.

%%HTML <svg width="800" height="300"> <circle cx="150" cy="180" r="80" fill-opacity="0.2" stroke="black" stroke-width="2" fill="blue" /> <circle cx="200" cy="100" r="80" fill-opacity="0.2" stroke="black" stroke-width="2" fill="red" /> <circle cx="100" cy="100" r="80" fill-opacity="0.2" stroke="black" stroke-width="2" fill="green" /> <line x1="150" y1="125" x2="300" y2="150" stroke="black" stroke-width="2" fill="black" stroke-dasharray="5,3"/> <text x="300" y="165" font-family="Verdana" font-size="35">Everything but this!</text> </svg>
def answer_two(): # YOUR CODE HERE # raise NotImplementedError() Energy = pd.read_excel('assets/Energy Indicators.xls',na_values=["..."],header = None,skiprows=18,skipfooter= 38,usecols=[2,3,4,5],names=['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']) Energy['Energy Supply'] = Energy['Energy Supply'].apply(lambda x: x*1000000) Energy['Country'] = Energy['Country'].str.replace(r" \(.*\)","") Energy['Country'] = Energy['Country'].str.replace(r"\d*","") Energy['Country'] = Energy['Country'].replace({'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'}) GDP = pd.read_csv('assets/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'}) ScimEn = pd.read_excel('assets/scimagojr-3.xlsx') inner1 = pd.merge(ScimEn,Energy,how="inner",left_on="Country",right_on="Country") GDP.rename(columns = {"Country Name":"Country"},inplace=True) GDP = GDP.loc[:,['2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015',"Country"]] inner2 = pd.merge(inner1,GDP,how="inner",left_on="Country",right_on="Country").set_index("Country") outer1 = pd.merge(ScimEn,Energy,how="outer",left_on="Country",right_on="Country") outer2 = pd.merge(outer1,GDP,how="outer",left_on="Country",right_on="Country").set_index("Country") return len(outer2)-len(inner2)
assert type(answer_two()) == int, "Q2: You should return an int number!"
answer_two()
156

Question 3

What are the top 15 countries for average GDP over the last 10 years?

This function should return a Series named avgGDP with 15 countries and their average GDP sorted in descending order.

test code

info=answer_one() info
info[["2006","2007","2008","2009","2010","2011","2012","2013","2014","2015"]].apply(np.mean,axis = 1).sort_values(ascending = False)
Country United States 1.536434e+13 China 6.348609e+12 Japan 5.542208e+12 Germany 3.493025e+12 France 2.681725e+12 United Kingdom 2.487907e+12 Brazil 2.189794e+12 Italy 2.120175e+12 India 1.769297e+12 Canada 1.660647e+12 Russian Federation 1.565459e+12 Spain 1.418078e+12 Australia 1.164043e+12 South Korea 1.106715e+12 Iran 4.441558e+11 dtype: float64

answer

def answer_three(): # YOUR CODE HERE # raise NotImplementedError() info=answer_one() return info[["2006","2007","2008","2009","2010","2011","2012","2013","2014","2015"]].apply(np.mean,axis = 1).sort_values(ascending = False)
assert type(answer_three()) == pd.Series, "Q3: You should return a Series!"
answer_three()
Country United States 1.536434e+13 China 6.348609e+12 Japan 5.542208e+12 Germany 3.493025e+12 France 2.681725e+12 United Kingdom 2.487907e+12 Brazil 2.189794e+12 Italy 2.120175e+12 India 1.769297e+12 Canada 1.660647e+12 Russian Federation 1.565459e+12 Spain 1.418078e+12 Australia 1.164043e+12 South Korea 1.106715e+12 Iran 4.441558e+11 dtype: float64

Question 4

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.

test code

info=answer_three() info.index[5]
'United Kingdom'
info=answer_one() info['avgGDP']=info[["2006","2007","2008","2009","2010","2011","2012","2013","2014","2015"]].apply(np.mean,axis = 1) info.sort_values(['avgGDP'],ascending = False,inplace=True) g6=info.index[5] info.loc[g6]["2015"]-info.loc[g6]["2006"] info.iloc[5]['2015']-info.iloc[5]['2006']
246702696075.3999

answer

def answer_four(): # YOUR CODE HERE # raise NotImplementedError() info=answer_one() info['avgGDP']=info[["2006","2007","2008","2009","2010","2011","2012","2013","2014","2015"]].apply(np.mean,axis = 1) info.sort_values(['avgGDP'],ascending = False,inplace=True) # g6=info.index[5] # info.loc[g6]["2015"]-info.loc[g6]["2006"] return info.iloc[5]['2015']-info.iloc[5]['2006']
# Cell for autograder.
answer_four()
246702696075.3999

Question 5

What is the mean energy supply per capita?

This function should return a single number.

info = answer_one() # info type(info['Energy Supply per Capita'].mean()) float(info['Energy Supply per Capita'].mean())
157.6
def answer_five(): # YOUR CODE HERE # raise NotImplementedError() info = answer_one() return info['Energy Supply per Capita'].mean() # return float(info['Energy Supply per Capita'].mean())
# Cell for autograder.
answer_five()
157.6

Question 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.

info = answer_one() # info['% Renewable'] result=info.sort_values(by='% Renewable', ascending=False).iloc[0] (result.name,result['% Renewable'])
('Brazil', 69.64803)
def answer_six(): # YOUR CODE HERE # raise NotImplementedError() info = answer_one() result=info.sort_values(by='% Renewable', ascending=False).iloc[0] return (result.name,result['% Renewable'])
assert type(answer_six()) == tuple, "Q6: You should return a tuple!" assert type(answer_six()[0]) == str, "Q6: The first element in your result should be the name of the country!"
answer_six()
('Brazil', 69.64803)

Question 7

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.

test code

info = answer_one() info
info = answer_one() info['Citation ratio']=info['Self-citations']/info['Citations'] result=info.sort_values(by='Citation ratio', ascending=False).iloc[0] (result.name,result['Citation ratio'])
('China', 0.6893126179389422)

answer

def answer_seven(): # YOUR CODE HERE # raise NotImplementedError() info = answer_one() info['Citation ratio']=info['Self-citations']/info['Citations'] result=info.sort_values(by='Citation ratio', ascending=False).iloc[0] return (result.name,result['Citation ratio'])
assert type(answer_seven()) == tuple, "Q7: You should return a tuple!" assert type(answer_seven()[0]) == str, "Q7: The first element in your result should be the name of the country!"
answer_seven()
('China', 0.6893126179389422)

Question 8

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 the name of the country

info = answer_one() info
info = answer_one() (info['Energy Supply']/info['Energy Supply per Capita']).sort_values(ascending=False).index[2] # info.sort_values(by='Population', ascending=False).index[2]
'United States'
def answer_eight(): # YOUR CODE HERE # raise NotImplementedError() info = answer_one() return (info['Energy Supply']/info['Energy Supply per Capita']).sort_values(ascending=False).index[2]
assert type(answer_eight()) == str, "Q8: You should return the name of the country!"
answer_eight()
'United States'

Question 9

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)

info = answer_one() info
Top15 = answer_one() Top15['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita'] Top15['Citable docs per Capita'] = Top15['Citable documents'] / Top15['PopEst'] # Top15[['Energy Supply per Capita', 'Citable docs per Capita']].corr(method='pearson')['Energy Supply per Capita', 'Citable docs per Capita'] Top15['Citable docs per Capita'].corr(Top15['Energy Supply per Capita'])
0.7940010435442942
Top15[['Citable docs per Capita','Energy Supply per Capita']].corr(method='pearson').iloc[0,1]
0.7940010435442943
def answer_nine(): # YOUR CODE HERE # raise NotImplementedError() Top15 = answer_one() Top15['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita'] Top15['Citable docs per Capita'] = Top15['Citable documents'] / Top15['PopEst'] return Top15['Citable docs per Capita'].corr(Top15['Energy Supply per Capita'])
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])
plot9()
Image in a Jupyter notebook
assert answer_nine() >= -1. and answer_nine() <= 1., "Q9: A valid correlation should between -1 to 1!"
answer_nine()
0.7940010435442942

Question 10

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.

Top15 = answer_one() Rmedian=Top15["% Renewable"].median() Top15["HighRenew"]= Top15["% Renewable"].apply(lambda x:0 if x<Rmedian else 1 ) Top15["HighRenew"]
Country China 1 United States 0 Japan 0 United Kingdom 0 Russian Federation 1 Canada 1 Germany 1 India 0 France 1 South Korea 0 Italy 1 Spain 1 Iran 0 Australia 0 Brazil 1 Name: HighRenew, dtype: int64
def answer_ten(): # YOUR CODE HERE # raise NotImplementedError() Top15 = answer_one() Rmedian=Top15["% Renewable"].median() Top15["HighRenew"]= Top15["% Renewable"].apply(lambda x:0 if x<Rmedian else 1 ) return Top15["HighRenew"]
assert type(answer_ten()) == pd.Series, "Q10: You should return a Series!"
answer_ten()
Country China 1 United States 0 Japan 0 United Kingdom 0 Russian Federation 1 Canada 1 Germany 1 India 0 France 1 South Korea 0 Italy 1 Spain 1 Iran 0 Australia 0 Brazil 1 Name: HighRenew, dtype: int64

Question 11

Use the following dictionary to group the Countries by Continent, then create a DataFrame 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.

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']

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['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita'] Top15['Continent'] = pd.Series(ContinentDict) Top15.groupby('Continent')['PopEst'].agg([np.size,np.sum, np.mean, np.std]) # {'size': np.size, 'sum': np.sum, 'mean': np.mean, 'std': np.std}) # for group, frame in Top15.groupby('Continent'): # print(group) # print(frame) # Top15
def answer_eleven(): # YOUR CODE HERE # raise NotImplementedError() 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['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita'] Top15['Continent'] = pd.Series(ContinentDict) return Top15.groupby('Continent')['PopEst'].agg([np.size,np.sum, np.mean, np.std])
assert type(answer_eleven()) == pd.DataFrame, "Q11: You should return a DataFrame!" assert answer_eleven().shape[0] == 5, "Q11: Wrong row numbers!" assert answer_eleven().shape[1] == 4, "Q11: Wrong column numbers!"
answer_eleven()

Question 12

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.

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'] = pd.Series(ContinentDict) Top15['% Renewable']=pd.cut(Top15['% Renewable'],5) Top15.groupby(['Continent','% Renewable']).size() # Top15.groupby(['Continent','% Renewable'])['Continent'].agg(np.size).dropna()
Continent % Renewable Asia (2.212, 15.753] 4 (15.753, 29.227] 1 (29.227, 42.701] 0 (42.701, 56.174] 0 (56.174, 69.648] 0 Australia (2.212, 15.753] 1 (15.753, 29.227] 0 (29.227, 42.701] 0 (42.701, 56.174] 0 (56.174, 69.648] 0 Europe (2.212, 15.753] 1 (15.753, 29.227] 3 (29.227, 42.701] 2 (42.701, 56.174] 0 (56.174, 69.648] 0 North America (2.212, 15.753] 1 (15.753, 29.227] 0 (29.227, 42.701] 0 (42.701, 56.174] 0 (56.174, 69.648] 1 South America (2.212, 15.753] 0 (15.753, 29.227] 0 (29.227, 42.701] 0 (42.701, 56.174] 0 (56.174, 69.648] 1 dtype: int64
def answer_twelve(): # YOUR CODE HERE # raise NotImplementedError() 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'] = pd.Series(ContinentDict) Top15['% Renewable']=pd.cut(Top15['% Renewable'],5) return Top15.groupby(['Continent','% Renewable'])['Continent'].agg(np.size).dropna()
assert type(answer_twelve()) == pd.Series, "Q12: You should return a Series!" assert len(answer_twelve()) == 9, "Q12: Wrong result numbers!"
answer_twelve()
Continent % Renewable Asia (2.212, 15.753] 4.0 (15.753, 29.227] 1.0 Australia (2.212, 15.753] 1.0 Europe (2.212, 15.753] 1.0 (15.753, 29.227] 3.0 (29.227, 42.701] 2.0 North America (2.212, 15.753] 1.0 (56.174, 69.648] 1.0 South America (56.174, 69.648] 1.0 Name: Continent, dtype: float64

Question 13

Convert the Population Estimate series to a string with thousands separator (using commas). Use all significant digits (do not round the results).

e.g. 12345678.90 -> 12,345,678.90

This function should return a series PopEst whose index is the country name and whose values are the population estimate string

Top15 = answer_one() Top15['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita'] Top15['PopEst'].apply('{:,}'.format)
Country China 1,367,645,161.2903225 United States 317,615,384.61538464 Japan 127,409,395.97315437 United Kingdom 63,870,967.741935484 Russian Federation 143,500,000.0 Canada 35,239,864.86486486 Germany 80,369,696.96969697 India 1,276,730,769.2307692 France 63,837,349.39759036 South Korea 49,805,429.864253394 Italy 59,908,256.880733944 Spain 46,443,396.2264151 Iran 77,075,630.25210084 Australia 23,316,017.316017315 Brazil 205,915,254.23728815 Name: PopEst, dtype: object
def answer_thirteen(): # YOUR CODE HERE # raise NotImplementedError() Top15 = answer_one() Top15['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita'] return Top15['PopEst'].apply('{:,}'.format)
assert type(answer_thirteen()) == pd.Series, "Q13: You should return a Series!" assert len(answer_thirteen()) == 15, "Q13: Wrong result numbers!"
answer_thirteen()
Country China 1,367,645,161.2903225 United States 317,615,384.61538464 Japan 127,409,395.97315437 United Kingdom 63,870,967.741935484 Russian Federation 143,500,000.0 Canada 35,239,864.86486486 Germany 80,369,696.96969697 India 1,276,730,769.2307692 France 63,837,349.39759036 South Korea 49,805,429.864253394 Italy 59,908,256.880733944 Spain 46,443,396.2264151 Iran 77,075,630.25210084 Australia 23,316,017.316017315 Brazil 205,915,254.23728815 Name: PopEst, dtype: object

Optional

Use the built in function plot_optional() to see an example visualization.

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.")
plot_optional()
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.
Image in a Jupyter notebook