【深耕 Python】Data Science with Python 数据科学(15)pandas 数据处理(六):书385页练习题

写在前面

关于数据科学环境的建立,可以参考我的博客:

【深耕 Python】Data Science with Python 数据科学(1)环境搭建

往期数据科学博文:

【深耕 Python】Data Science with Python 数据科学(2)jupyter-lab和numpy数组

【深耕 Python】Data Science with Python 数据科学(3)Numpy 常量、函数和线性空间

【深耕 Python】Data Science with Python 数据科学(4)(书337页)练习题及解答

【深耕 Python】Data Science with Python 数据科学(5)Matplotlib可视化(1)

【深耕 Python】Data Science with Python 数据科学(6)Matplotlib可视化(2)

【深耕 Python】Data Science with Python 数据科学(7)书352页练习题

【深耕 Python】Data Science with Python 数据科学(8)pandas数据结构:Series和DataFrame

【深耕 Python】Data Science with Python 数据科学(9)书361页练习题

【深耕 Python】Data Science with Python 数据科学(10)pandas 数据处理(一)

【深耕 Python】Data Science with Python 数据科学(11)pandas 数据处理(二)

【深耕 Python】Data Science with Python 数据科学(12)pandas 数据处理(三)

【深耕 Python】Data Science with Python 数据科学(13)pandas 数据处理(四):书377页练习题

【深耕 Python】Data Science with Python 数据科学(14)pandas 数据处理(五):泰坦尼克号亡魂 Perished Souls on “RMS Titanic”

代码说明: 由于实机运行的原因,可能省略了某些导入(import)语句。

本期练习题基于上期“泰坦尼克号”乘客生还的数据分析。

Exercises

1. Confirm using the code in Listing 11.21 that the Titanic survival rate for female passengers in third class was 50%. How does this compare to the survival rate for male passengers in first class?

Answer in Python:

import pandas as pd

URL = "https://learnenough.s3.amazonaws.com/titanic.csv"
titanic = pd.read_csv(URL)
print(titanic[(titanic["Sex"] == "female") & (titanic["Pclass"] == 3)]["Survived"].mean())
print(titanic[(titanic["Sex"] == "male") & (titanic["Pclass"] == 1)]["Survived"].mean())

程序输出:

0.5  # 三等舱,女性,生还率
0.36885245901639346  # 一等舱,男性,生还率

2. Make two versions of the bar chart for Titanic survival rates by age shown in Figure 11.31, one each for male passengers and female passengers. Hint: Define sex-specific variables as shown in Listing 11.22 and redo the analysis after Listing 11.20 separately for the male_ and female_ variables.

Answer in Python:

# plot 1
plt.figure(1)
sorted_male_age = valid_male_ages.sort_values(by="Age")
sorted_male_age["Age range"] = pd.cut(sorted_male_age["Age"], 7)
male_survival_rates = sorted_male_age.groupby("Age range")["Survived"].mean()
male_survival_rates.plot.bar()
plt.title("Survival Rate for Male Passengers")
plt.ylabel("Survival Rate")
plt.grid()

# plot 2
plt.figure(2)
sorted_female_age = valid_female_ages.sort_values(by="Age")
sorted_female_age["Age range"] = pd.cut(sorted_female_age["Age"], 7)
female_survival_rates = sorted_female_age.groupby("Age range")["Survived"].mean()
female_survival_rates.plot.bar()
plt.title("Survival Rate for Female Passengers")
plt.ylabel("Survival Rate")
plt.grid()

程序输出:

柱状图1(不同年龄男性生还率):

在这里插入图片描述

柱状图2(不同年龄女性生还率):

在这里插入图片描述
3. Widener Library at Harvard University was built by Eleanor Elkins Widener, who survived the Titanic sinking, to honor her son Harry, who did not. Using a substring search similar to the one in Listing 11.14, show that Harry is in our Titanic dataset, but Eleanor is not. How old was Harry when he died? Hint: You can search for names containing the substring “Widener”, but because we set “Name” as the index column in Listing 11.19, you should use titanic.index instead of titanic[“Name”] in the search.

Answer in Python:

titanic = pd.read_csv(URL, index_col="Name")
print(titanic.loc[titanic.index.str.contains("Widener")])

程序输出:

# 唯一姓名中含有“Widener”的乘客为Harry Elkins Widener,男性,年龄为27岁,一等舱,并未生还。
                                 PassengerId  Survived  Pclass   Sex   Age  SibSp  \
Name                                                                          
Widener, Mr. Harry Elkins          378         0           1     male  27.0      0   

                           Parch  Ticket   Fare Cabin Embarked  
Name                                                            
Widener, Mr. Harry Elkins      2  113503  211.5   C82        C

参考文献 Reference

《Learn Enough Python to be Dangerous——Software Development, Flask Web Apps, and Beginning Data Science with Python》, Michael Hartl, Boston, Pearson, 2023.

  • 30
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不是AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值