文章目录
问题背景
以下代码读取了奥运数据集(olympics.csv)并进行了数据清理。该数据集是从历届奥运会奖牌上的Wikipedia条目获取而来的。
这些列按参加
# Summer:夏季运动会数量,夏季运动会奖牌数,# Winter:冬季运动会数量,冬季运动会奖牌总数,# Games:参加的总次数,奖牌总数
的顺序来组织。
import pandas as pd
import numpy as np
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\(") # 以'('分割索引
df.index = names_ids.str[0] # 元素[0]是国家名字
df["ID"] = names_ids.str[1].str[:3] # 元素[1] 国家缩写 (国家名字的前三个字母)
df = df.drop("Totals")
df.head()
# Summer | Gold | Silver | Bronze | Total | # Winter | Gold.1 | Silver.1 | Bronze.1 | Total.1 | # Games | Gold.2 | Silver.2 | Bronze.2 | Combined total | ID | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Afghanistan | 13 | 0 | 0 | 2 | 2 | 0 | 0 | 0 | 0 | 0 | 13 | 0 | 0 | 2 | 2 | AFG |
Algeria | 12 | 5 | 2 | 8 | 15 | 3 | 0 | 0 | 0 | 0 | 15 | 5 | 2 | 8 | 15 | ALG |
Argentina | 23 | 18 | 24 | 28 | 70 | 18 | 0 | 0 | 0 | 0 | 41 | 18 | 24 | 28 | 70 | ARG |
Armenia | 5 | 1 | 2 | 9 | 12 | 6 | 0 | 0 | 0 | 0 | 11 | 1 | 2 | 9 | 12 | ARM |
Australasia | 2 | 3 | 4 | 5 | 12 | 0 | 0 | 0 | 0 | 0 | 2 | 3 | 4 | 5 | 12 | ANZ |
使用此数据集回答以下问题:
问题 0 (样例):给出第一个国家的获奖信息.
这个函数应该返回Series.
def answer_zero():
# 这个函数以Series的类型返回关于Afghanistan的信息.
return df.iloc[0]
answer_zero()
# Summer 13
Gold 0
Silver 0
Bronze 2
Total 2
# Winter 0
Gold.1 0
Silver.1 0
Bronze.1 0
Total.1 0
# Games 13
Gold.2 0
Silver.2 0
Bronze.2 2
Combined total 2
ID AFG
Name: Afghanistan, dtype: object
问题 1:哪一个国家在夏季运动会中获得最多的金牌?
这个函数应该以字符串类型返回国家的名字.
def answer_one():
# 返回夏季奥运会获得金牌最多的国家
a = df['Gold'].idxmax()#idxmax()函数返回此列最大值的下标
return a
pass
answer_one()
'United States'
问题 2:哪一个国家的夏季运动会金牌数与冬季运动会金牌数之差最大?
这个函数应该以字符串类型返回国家的名字.
def answer_two():
# 夏季运动会金牌数与冬季运动会金牌数之差最大的国家
# (未说明情况,差值取绝对值)
b = np.abs(df['Gold'] - df['Gold.1']).idxmax()# idxmax()函数返回此算式最大值的下标
return b
pass
answer_two()
'United States'
问题 3:哪一个国家的夏季运动会与冬季运动会的金牌数之差相比于金牌总数最大?
提示:可参照下式计算
S
u
m
m
e
r
G
o
l
d
−
W
i
n
t
e
r
G
o
l
d
T
o
t
a
l
G
o
l
d
\frac{Summer~Gold - Winter~Gold}{Total~Gold}
Total GoldSummer Gold−Winter Gold
另外,我们只考虑那些在夏季运动会和冬季运动会都至少获得一块金牌的国家.
这个函数应该以字符串类型返回国家的名字.
def answer_three():
# 夏季运动会与冬季运动会的金牌数之差相比于金牌总数最大的国家
# 易得,夏季运动会得到金牌且冬季运动会没得到金牌的国家为1,比值最大
df1 = df[(df['Gold'] > 0) & (df['Gold.1'] > 0)]# 新建df1文件取出符合的国家
c = ((df1['Gold'] - df1['Gold.1']) / df1['Gold.2']).idxmax()# idxmax()函数返回此算式最大值的下标
return c
pass
answer_three()
'Bulgaria'
问题 4:写一个函数创建名为"Points"的Series对象,此对象用于计算奖牌积分,其中金牌(Gold.2
)计3分,银牌(Silver.2
)计2分,铜牌 (Bronze.2
) 计1分.
这个函数应该返回一个长度为146的“name”为Points
的Series对象
def answer_four():
# 函数计分表
data = (df['Gold.2'] * 3 + df['Silver.2'] * 2 + df["Bronze.2"] * 1) # 按照题目要求设计数据的算法
d = pd.Series(data = data) # 新建Series对象,导入目标数据
return d
pass
answer_four()
Afghanistan 2
Algeria 27
Argentina 130
Armenia 16
Australasia 22
...
Yugoslavia 171
Independent Olympic Participants 4
Zambia 3
Zimbabwe 18
Mixed team 38
Length: 146, dtype: int64