u1s1,这门课的assignment还是有点难度的,特别是assigment4(哀怨),放给大家参考啦~
有时间(需求)就把所有代码放到github上(好担心被河蟹啊)
先放下该课程相关链接:
Coursera | Introduction to Data Science in Python(University of Michigan)| quiz答案
Coursera | Introduction to Data Science in Python(University of Michigan)| Assignment1
Coursera | Introduction to Data Science in Python(University of Michigan)| Assignment2
Coursera | Introduction to Data Science in Python(University of Michigan)| Assignment3
Coursera | Introduction to Data Science in Python(University of Michigan)| Assignment4
嘿,顺便推广下自己的博客,以后CSDN的文章都会放到自己的博客的。
Coursera | Introduction to Data Science in Python(University of Michigan)| Assignment3
assignment3开始难度暴增,到4就爽翻了。一共13题,刺激。
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 (assets/Energy%20Indicators.xls) 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):
"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 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. 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 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”.
Code
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!"
结果
| 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 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Country | ||||||||||||||||||||
| China | 1 | 127050 | 126767 | 597237 | 411683 | 4.70 | 138 | 1.271910e+11 | 93.0 | 19.754910 | 3.992331e+12 | 4.559041e+12 | 4.997775e+12 | 5.459247e+12 | 6.039659e+12 | 6.612490e+12 | 7.124978e+12 | 7.672448e+12 | 8.230121e+12 | 8.797999e+12 |
| United States | 2 | 96661 | 94747 | 792274 | 265436 | 8.20 | 230 | 9.083800e+10 | 286.0 | 11.570980 | 1.479230e+13 | 1.505540e+13 | 1.501149e+13 | 1.459484e+13 | 1.496437e+13 | 1.520402e+13 | 1.554216e+13 | 1.577367e+13 | 1.615662e+13 | 1.654857e+13 |
| Japan | 3 | 30504 | 30287 | 223024 | 61554 | 7.31 | 134 | 1.898400e+10 | 149.0 | 10.232820 | 5.496542e+12 | 5.617036e+12 | 5.558527e+12 | 5.251308e+12 | 5.498718e+12 | 5.473738e+12 | 5.569102e+12 | 5.644659e+12 | 5.642884e+12 | 5.669563e+12 |
| United Kingdom | 4 | 20944 | 20357 | 206091 | 37874 | 9.84 | 139 | 7.920000e+09 | 124.0 | 10.600470 | 2.419631e+12 | 2.482203e+12 | 2.470614e+12 | 2.367048e+12 | 2.403504e+12 | 2.450911e+12 | 2.479809e+12 | 2.533370e+12 | 2.605643e+12 | 2.666333e+12 |
| Russian Federation | 5 | 18534 | 18301 | 34266 | 12422 | 1.85 | 57 | 3.070900e+10 | 214.0 | 17.288680 | 1.385793e+12 | 1.504071e+12 | 1.583004e+12 | 1.459199e+12 | 1.524917e+12</ |

最低0.47元/天 解锁文章
5377

被折叠的 条评论
为什么被折叠?



