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)| 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
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.
1 | import pandas as pd |
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):
1 | "Republic of Korea": "South Korea", |
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:
1 | "Korea, Rep.": "South Korea", |
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
1 | def answer_one(): |
1 | assert type(answer_one()) == pd.DataFrame, "Q1: You should return a DataFrame!" |
结果
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 | 1.589943e+12 | 1.645876e+12 | 1.666934e+12 | 1.678709e+12 | 1.616149e+12 |
Canada | 6 | 17899 | 17620 | 215003 | 40930 | 12.01 | 149 | 1.043100e+10 | 296.0 | 61.945430 | 1.564469e+12 | 1.596740e+12 | 1.612713e+12 | 1.565145e+12 | 1.613406e+12 | 1.664087e+12 | 1.693133e+12 | 1.730688e+12 | 1.773486e+12 | 1.792609e+12 |
Germany | 7 | 17027 | 16831 | 140566 | 27426 | 8.26 | 126 | 1.326100e+10 | 165.0 | 17.901530 | 3.332891e+12 | 3.441561e+12 | 3.478809e+12 | 3.283340e+12 | 3.417298e+12 | 3.542371e+12 | 3.556724e+12 | 3.567317e+12 | 3.624386e+12 | 3.685556e+12 |
India | 8 | 15005 | 14841 | 128763 | 37209 | 8.58 | 115 | 3.319500e+10 | 26.0 | 14.969080 | 1.265894e+12 | 1.374865e+12 | 1.428361e+12 | 1.549483e+12 | 1.708459e+12 | 1.821872e+12 | 1.924235e+12 | 2.051982e+12 | 2.200617e+12 | 2.367206e+12 |
France | 9 | 13153 | 12973 | 130632 | 28601 | 9.93 | 114 | 1.059700e+10 | 166.0 | 17.020280 | 2.607840e+12 | 2.669424e+12 | 2.674637e+12 | 2.595967e+12 | 2.646995e+12 | 2.702032e+12 | 2.706968e+12 | 2.722567e+12 | 2.729632e+12 | 2.761185e+12 |
South Korea | 10 | 11983 | 11923 | 114675 | 22595 | 9.57 | 104 | 1.100700e+10 | 221.0 | 2.279353 | 9.410199e+11 | 9.924316e+11 | 1.020510e+12 | 1.027730e+12 | 1.094499e+12 | 1.134796e+12 | 1.160809e+12 | 1.194429e+12 | 1.234340e+12 | 1.266580e+12 |
Italy | 11 | 10964 | 10794 | 111850 | 26661 | 10.20 | 106 | 6.530000e+09 | 109.0 | 33.667230 | 2.202170e+12 | 2.234627e+12 | 2.211154e+12 | 2.089938e+12 | 2.125185e+12 | 2.137439e+12 | 2.077184e+12 | 2.040871e+12 | 2.033868e+12 | 2.049316e+12 |
Spain | 12 | 9428 | 9330 | 123336 | 23964 | 13.08 | 115 | 4.923000e+09 | 106.0 | 37.968590 | 1.414823e+12 | 1.468146e+12 | 1.484530e+12 | 1.431475e+12 | 1.431673e+12 | 1.417355e+12 | 1.380216e+12 | 1.357139e+12 | 1.375605e+12 | 1.419821e+12 |
Iran | 13 | 8896 | 8819 | 57470 | 19125 | 6.46 | 72 | 9.172000e+09 | 119.0 | 5.707721 | 3.895523e+11 | 4.250646e+11 | 4.289909e+11 | 4.389208e+11 | 4.677902e+11 | 4.853309e+11 | 4.532569e+11 | 4.445926e+11 | 4.639027e+11 | NaN |
Australia | 14 | 8831 | 8725 | 90765 | 15606 | 10.28 | 107 | 5.386000e+09 | 231.0 | 11.810810 | 1.021939e+12 | 1.060340e+12 | 1.099644e+12 | 1.119654e+12 | 1.142251e+12 | 1.169431e+12 | 1.211913e+12 | 1.241484e+12 | 1.272520e+12 | 1.301251e+12 |
Brazil | 15 | 8668 | 8596 | 60702 | 14396 | 7.00 | 86 | 1.214900e+10 | 59.0 | 69.648030 | 1.845080e+12 | 1.957118e+12 | 2.056809e+12 | 2.054215e+12 | 2.208872e+12 | 2.295245e+12 | 2.339209e+12 | 2.409740e+12 | 2.412231e+12 | 2.319423e+12 |
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.
1 | %%HTML |
Code
1 | def answer_two(): |
1 | assert type(answer_two()) == int, "Q2: You should return an int number!" |
结果
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.
Code
1 | def answer_three(): |
1 | assert type(answer_three()) == pd.Series, "Q3: You should return a Series!" |
结果
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.
Code
1 | def answer_four(): |
结果
246702696075.3999
Question 5
What is the mean energy supply per capita?
This function should return a single number.
1 | def 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.
Code
1 | def answer_six(): |
1 | assert type(answer_six()) == tuple, "Q6: You should return a tuple!" |
结果
('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.
Code
1 | def answer_seven(): |
1 | assert type(answer_seven()) == tuple, "Q7: You should return a tuple!" |
结果
('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
Code
1 | def answer_eight(): |
1 | assert type(answer_eight()) == str, "Q8: You should return the name of the country!" |
结果
'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)
Code
1 | def answer_nine(): |
1 | def plot9(): |
1 | assert answer_nine() >= -1. and answer_nine() <= 1., "Q9: A valid correlation should between -1 to 1!" |
结果
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.
Code
1 | def answer_ten(): |
1 | assert type(answer_ten()) == pd.Series, "Q10: You should return a Series!" |
结果
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.
1 | ContinentDict = {'China':'Asia', |
This function should return a DataFrame with index named Continent ['Asia', 'Australia', 'Europe', 'North America', 'South America']
and columns ['size', 'sum', 'mean', 'std']
Code
1 | def answer_eleven(): |
1 | assert type(answer_eleven()) == pd.DataFrame, "Q11: You should return a DataFrame!" |
结果
size | sum | mean | std | |
---|---|---|---|---|
Continent | ||||
Asia | 5.0 | 2.898666e+09 | 5.797333e+08 | 6.790979e+08 |
Australia | 1.0 | 2.331602e+07 | 2.331602e+07 | NaN |
Europe | 6.0 | 4.579297e+08 | 7.632161e+07 | 3.464767e+07 |
North America | 2.0 | 3.528552e+08 | 1.764276e+08 | 1.996696e+08 |
South America | 1.0 | 2.059153e+08 | 2.059153e+08 | NaN |
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.
Code
1 | def answer_twelve(): |
1 | assert type(answer_twelve()) == pd.Series, "Q12: You should return a Series!" |
结果
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
Code
1 | def answer_thirteen(): |
1 | assert type(answer_thirteen()) == pd.Series, "Q13: You should return a Series!" |
结果
Optional
Use the built in function plot_optional()
to see an example visualization.
Code
1 | def plot_optional(): |
结果
大家其他还有需要的就在评论留言哦 :) 欢迎讨论分享~