Coursera Introduction to Data Science in Python Assignment2
初学python数据分析,以下是Coursera Introduction to Data Science in Python 的编程作业,小白写代码,不够严谨,还请大家一起讨论,共同进步
Part 1
The following code loads the olympics dataset (olympics.csv), which was derrived from the Wikipedia entry on All Time Olympic Games Medals, and does some basic data cleaning.
The columns are organized as # of Summer games, Summer medals, # of Winter games, Winter medals, total # number of games, total # of medals. Use this dataset to answer the questions below.
import pandas as pd
df = pd.read_csv('olympics.csv', index_col=0, skiprows=1)
for col in df.columns:
if col[:2]=='01':
df.rename(columns={col:'Gold'+col[4:]}, inplace=True)
if col[:2]=='02':
df.rename(columns={col:'Silver'+col[4:]}, inplace=True)
if col[:2]=='03':
df.rename(columns={col:'Bronze'+col[4:]}, inplace=True)
if col[:1]=='№':
df.rename(columns={col:'#'+col[1:]}, inplace=True)
names_ids = df.index.str.split('\s\(') # split the index by '('
df.index = names_ids.str[0] # the [0] element is the country name (new index)
df['ID'] = names_ids.str[1].str[:3] # the [1] element is the abbreviation or ID (take first 3 characters from that)
df = df.drop('Totals')
df
Question 0 (Example)
What is the first country in df?
This function should return a Series.
# You should write your whole answer within the function provided. The autograder will call
# this function and compare the return value against the correct solution value
def answer_zero():
# This function returns the row for Afghanistan, which is a Series object. The assignment
# question description will tell you the general format the autograder is expecting
return df.iloc[0]
# You can examine what your function returns by calling it in the cell. If you have questions
# about the assignment formats, check out the discussion forums for any FAQs
answer_zero()
Question 1
Which country has won the most gold medals in summer games?
This function should return a single string value.
def answer_one():
return df[df['Gold'] == max(df['Gold'])].index[0]
answer_one()
Question 2
Which country had the biggest difference between their summer and winter gold medal counts?
This function should return a single string value.
def answer_two():
df['difference'] = abs(df['Gold']-df['Gold.1'])
return df[df['difference'] == max(df['difference'])].index[0]
answer_two()
Question 3
Which country has the biggest difference between their summer gold medal counts and winter gold medal counts relative to their total gold medal count?
Only include countries that have won at least 1 gold in both summer and winter.
This function should return a single string value.
def answer_three():
a = df.copy()
a = a.drop(a[(a['Gold'] & a['Gold.1']).isin([0])].index)
a['relative'] = abs((a['Gold']-a['Gold.1'])/a['Gold.2'])
return a[a['relative'] == max(a['relative'])].index[0]
answer_three()
Question 4
Write a function that creates a Series called "Points" which is a weighted value where each gold medal (Gold.2
) counts
for 3 points, silver medals (Silver.2
) for 2 points, and bronze medals (Bronze.2
)
for 1 point. The function should return only the column (a Series object) which you created, with the country names as indices.
This function should return a Series named Points
of length 146
def answer_four():
return pd.Series((df['Gold.2']*3)+(df['Silver.2']*2)+df['Bronze.2'],index=df.index)
answer_four()
Part 2
For the next set of questions, we will be using census data from the United States Census Bureau. Counties are political and geographic subdivisions of states in the United States. This dataset contains population data for counties and states in the US from 2010 to 2015. See this document for a description of the variable names.
The census dataset (census.csv) should be loaded as census_df. Answer questions using this as appropriate.
Question 5
Which state has the most counties in it? (hint: consider the sumlevel key carefully! You'll need this for future questions too...)
This function should return a single string value.
census_df = pd.read_csv('census.csv')
census_df.head(70)
def answer_five():
num = list(census_df['STATE'].unique())
count = 0
for i in num:
if(census_df.where(census_df['STATE']==i).count()[1] > count):
count = census_df.where(census_df['STATE']==i).count()[1]
j = i
name_Series = census_df[census_df['STATE']==j]['STNAME']
name = name_Series.iloc[1]
return name
answer_five()
Question 6
Only looking at the three most populous counties for each state, what are the three most populous states (in order of highest population to lowest population)? Use CENSUS2010POP
.
This function should return a list of string values.
def answer_six():
num = list(census_df['STATE'].unique())
census_df_1 = pd.DataFrame()
census_count = []
for i in num:
census_df_1=census_df_1.append(census_df[census_df['STATE']==i].iloc[1:].nlargest(3,'CENSUS2010POP'))
census_count.append(sum(census_df_1[census_df_1['STATE']==i]['CENSUS2010POP']))
census_count = pd.Series(census_count, index=census_df['STNAME'].unique())
a = list(census_count.nlargest(3).index)
return a
answer_six()
Question 7
Which county has had the largest absolute change in population within the period 2010-2015? (Hint: population values are stored in columns POPESTIMATE2010 through POPESTIMATE2015, you need to consider all six columns.)
e.g. If County Population in the 5 year period is 100, 120, 80, 105, 100, 130, then its largest change in the period would be |130-80| = 50.
This function should return a single string value.
def answer_seven():
c_copy = census_df[['SUMLEV','STATE','COUNTY','STNAME','CTYNAME','POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']]
c_copy['change'] = c_copy[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].max(axis=1)-c_copy[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].min(axis=1)
c_copy = c_copy.drop(c_copy[c_copy['COUNTY']==0].index)
a = c_copy['change']
a.index = c_copy['CTYNAME']
return a.idxmax()
answer_seven()
Question 8
In this datafile, the United States is broken up into four regions using the "REGION" column.
Create a query that finds the counties that belong to regions 1 or 2, whose name starts with 'Washington', and whose POPESTIMATE2015 was greater than their POPESTIMATE 2014.
This function should return a 5x2 DataFrame with the columns = ['STNAME', 'CTYNAME'] and the same index ID as the census_df (sorted ascending by index).
def answer_eight():
a_copy = census_df[census_df['REGION'].isin([1,2])]
a_copy = a_copy[a_copy['CTYNAME'].str.startswith('Washington')]
a_copy = a_copy[a_copy['POPESTIMATE2015'] > a_copy['POPESTIMATE2014']]
a_copy = a_copy[['STNAME', 'CTYNAME']]
return a_copy
answer_eight()
推荐阅读
-
introduction to data science w4
-
Coursera Introduction to Data Science in Python Assignment2
-
Coursera | Applied Plotting, Charting & Data Representation in Python(UMich)| Assignment2
-
Coursera | Applied Plotting, Charting & Data Representation in Python(UMich)| Assignment2
-
Intro to Python for Data Science Learning 6 - NumPy
-
Python for Data Science
-
Python Data Science, NumPy 1
-
Data Science完整学习路径Python版
-
python for data science学习记录
-
Python Data Science Toolbox (Part 1)