Coursera Introduction to Data Science in Python Assignment2

这篇博客介绍了Coursera上Python数据科学课程的编程作业,涉及数据加载、数据清理以及使用olympics和census数据集进行数据分析的问题解答。包括找出夏季奥运会金牌最多的国家、夏季与冬季金牌数差异最大的国家、金牌比例相差最悬殊的国家,以及创建一个加权得分的Series。同时,对美国人口普查数据进行了分析,找出拥有最多县的州、人口最多的三个州以及人口变化最大的县,并查询特定区域的华盛顿县。
摘要由CSDN通过智能技术生成

初学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?

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值