我是靠谱客的博主 无私乌龟,最近开发中收集的这篇文章主要介绍Assignment 2 - Pandas Introduction 密歇根大学的python数据科学作业Assignment 2 - Pandas Introduction,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


这个作业做得我头发都要掉光了,网上的参考答案看起来又好复杂,所以我决定自己记录一下,代码不敢说是最简洁的,但是绝对是最适合新手的,毕竟我也是个小白

Assignment 2 - Pandas Introduction

All questions are weighted the same in this assignment.

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.head()

 

 # SummerGoldSilverBronzeTotal# WinterGold.1Silver.1Bronze.1Total.1# GamesGold.2Silver.2Bronze.2Combined totalID
Afghanistan13002200000130022AFG
Algeria1252815300001552815ALG
Argentina23182428701800004118242870ARG
Armenia512912600001112912ARM
Australasia23451200000234512ANZ

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['Gold'].idxmax()

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():
return ((df['Gold']-df['Gold.1']).abs()).idxmax()

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?

 

Summer Gold−Winter GoldTotal GoldSummer Gold−Winter GoldTotal Gold

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():
s = df[(df['Gold']>0) & (df['Gold.1']>0)]
answer = ((s['Gold']-s['Gold.1'])/(s['Gold.2'])).idxmax()
return answer

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():
Points = pd.Series(df['Gold.2']*3+df['Silver.2']*2+df['Bronze.2']*1, index=df.index)
return Points.rename('Points of length 146')

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()
SUMLEVREGIONDIVISIONSTATECOUNTYSTNAMECTYNAMECENSUS2010POPESTIMATESBASE2010POPESTIMATE2010...RDOMESTICMIG2011RDOMESTICMIG2012RDOMESTICMIG2013RDOMESTICMIG2014RDOMESTICMIG2015RNETMIG2011RNETMIG2012RNETMIG2013RNETMIG2014RNETMIG2015
0403610AlabamaAlabama477973647801274785161...0.002295-0.1931960.3810660.582002-0.4673691.0300150.8266441.3832821.7247180.712594
1503611AlabamaAutauga County545715457154660...7.242091-2.915927-3.0123492.265971-2.5307997.606016-2.626146-2.7220022.592270-2.187333
2503613AlabamaBaldwin County182265182265183193...14.83296017.64729321.84570519.24328717.19787215.84417618.55962722.72762620.31714218.293499
3503615AlabamaBarbour County274572745727341...-4.728132-2.500690-7.056824-3.904217-10.543299-4.874741-2.758113-7.167664-3.978583-10.543299
4503617AlabamaBibb County229152291922861...-5.527043-5.068871-6.201001-0.1775370.177258-5.088389-4.363636-5.4037290.7545331.107861

5 rows × 100 columns

def answer_five():
result = census_df[census_df['SUMLEV']==50].groupby('STNAME')['COUNTY'].sum()
return result.idxmax()

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():
rank = census_df[census_df['SUMLEV']==50].groupby('STNAME')['CENSUS2010POP'].apply(lambda x: x.nlargest(3).sum()).nlargest(3).index.values.tolist()
return rank

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():
population = census_df[census_df['SUMLEV'] == 50].loc[:,'POPESTIMATE2010':'POPESTIMATE2015']
change = population.max(axis=1) - population.min(axis=1)
return census_df['CTYNAME'][change.idxmax()]

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():
region = census_df[census_df['REGION'] < 3]
result = region[(region['CTYNAME'].str.startswith('Washington')) & ((region['POPESTIMATE2015']) > (region['POPESTIMATE2014']))].loc[:,('STNAME','CTYNAME')]
return result

题外话:之前向我提问的朋友们,由于年代久远我真的想不起来答案了,对不住各位;然后不知道为什么一个很喜欢的大神把我取关了,是因为我太坑了吗?综上,我以后一定好好努力,希望和大家共同进步!

最后

以上就是无私乌龟为你收集整理的Assignment 2 - Pandas Introduction 密歇根大学的python数据科学作业Assignment 2 - Pandas Introduction的全部内容,希望文章能够帮你解决Assignment 2 - Pandas Introduction 密歇根大学的python数据科学作业Assignment 2 - Pandas Introduction所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部