跳至主要內容

Go to canvas and download titanic.csv

AI悦创原创1v1python 1v1MA407伦敦政经LSE Home数据结构一对一留学生辅导留学生作业辅导1v1python 1v1MA407伦敦政经LSE Home数据结构一对一留学生辅导留学生作业辅导大约 21 分钟...约 6368 字

Notebook

使用命令行运行 Jupyter Notebook 的具体流程如下:

  1. 思考🤔:你想在哪里运行你的项目 or 你想把代码存储在哪里?「确定路径」
  2. 启动 CMD:在文件夹上面的路径中,直接输入 cmd 回车「Enter」目标🎯:为了更快速的到达目标路径。(还有多种方法,以后再讲)
  3. 确保安装了 Jupyter Notebook:
jupyter notebook
  1. 安装 Jupyter:
pip install jupyter notebook

题目

EN

Go to canvas and download titanic.csv

Data Dictionary

VariableDefinitionValues
survivalSurvival0 = No, 1 = Yes
pclassTicket class1 = 1st, 2 = 2nd, 3 = 3rd
sexSex
AgeAge in years
sibsp# of siblings / spouses aboard the Titanic
parch# of parents / children aboard the Titanic
ticketTicket number
farePassenger fare
cabinCabin number
embarkedPort of EmbarkationC = Cherbourg, Q = Queenstown, S = Southampton

Useful Pandas functions

.value_counts().nlargest()/nsmallest()
.size().rename()
loc[]/iloc[].qcut()
.at[]/iat[].to_excel()
.groupby().astype(str)
.agg().nlargest()/nsmallest()
.drop()np.where()
.isnull().to_numeric()
.apply()

Tasks (be careful about overwriting the dataframe! You may need to create a copy) *More advanced

  1. Convert PassengerId to a category

  2. Create a feature called “Family_size” that is the sum of siblings and parents/children

  3. Subsetting rows

    a. Find rows where Age > 10 and display the values for columns “Survived” and “Pclass”

    b. *Find the people with the 10 smallest Ages and compute the mean and standard deviation of their ages

  4. Sorting & counting values

    a. Who paid the highest fair?

    b. Which man who survived paid the highest fare?

    c. What are the top 5 oldest people in class 3?

    d. What was the percentage of men and women that died by point of embarcation?

  5. Cleaning data

    a. *Impute the cabin of (the oldest) Mr. Hickman as “B99” (hint: Google “accessor function str pandas”)

  6. Transforming data

    a) Create a new column “is_missing_age” if the row contains any missing values for Age. It should have 1 if missing any values, and 0 otherwise

    b) *Create a new column called “long_name” with .apply() that counts the letters in the name and if there are more than 20 returns “long name”

    c) Drop new the newly created column

  7. Grouping data

    d) What was the average fare by class?

    e) Who paid more on average for tickets? Men or women?

    f) *Bonus: Can you create a print(f'') that displays the results rounded to the nearest 2 digits?

    g) What is the total number of siblings for men? for women?

  8. Summarizing grouped data

    h) Use the .agg() function to find the mean and standard deviation of Ages for men and women who survived/did not survive

  9. Export a version with only complete cases (no missing values) with the name ‘complete_titanic.csv’ without index numbers. How many rows remain?

    i) How would you export it as an Excel file with the sheet name “passenger_list”?

Solution

1. 将 PassengerId 转为 category 类型

# 使用 astype 方法将 PassengerId 列的数据类型转换为'category'
titanic['PassengerId'] = titanic['PassengerId'].astype('category')
category 类型?

在 pandas 中,category 是一种特殊的数据类型,它用于表示具有固定数量不同值的数据列。这种数据类型特别适用于具有许多重复值的列,因为它可以大大减少内存的使用。它可以被视为在 pandas 中的枚举或因子类型。

例如,假设你有一个包含数百万行的数据集,其中一个列是国家名。尽管你有数百万行数据,但国家的实际数量可能只有几十个。这种情况下,使用 category 类型会比使用传统的字符串类型更加内存高效。

但在这个特定的上下文中,将 PassengerId 转换为 category 可能并不是一个理想的选择,因为 PassengerId 通常应该是一个唯一的标识符,每个值都是独特的,所以转换它为 category 类型可能并不会带来太多好处,可能还会增加计算的复杂性。这是一个错误,应该被更正。

2. 创建一个名为“Family_size”的特征,该特征是 siblings 和 parents/children 的总和

# 通过加法直接创建新列'Family_size'
titanic['Family_size'] = titanic['SibSp'] + titanic['Parch']
计算原理

这个代码:titanic['Family_size'] = titanic['SibSp'] + titanic['Parch'] 这个计算原理是什么?

这段代码的逻辑是基于数据字典中给出的定义来创建一个新的特征或列。在数据字典中:

  • SibSp 表示乘客在泰坦尼克号上的兄弟姐妹和配偶的数量。
  • Parch 表示乘客在泰坦尼克号上的父母和孩子的数量。

根据上述定义,一个人在船上的家庭大小可以通过将这两个值相加来计算,因为这两个值加在一起就代表了这个人的家庭成员总数。

因此,这段代码的目的是计算每个乘客的家庭总人数,并将这个值保存在一个新的列 Family_size 中。

例如,如果一个乘客有 1 个兄弟姐妹和 2 个孩子在船上,那么他的 SibSp 值为1,Parch 值为2。因此,Family_size 值为3,表示这个乘客的家庭总共有3个成员(除了他自己)在船上。

3. 子集筛选

a. 找到年龄大于 10 的行,并显示 “Survived” 和 “Pclass” 的值。

subset_a = titanic[titanic['Age'] > 10][['Survived', 'Pclass']]
原理解析

代码 subset_a = titanic[titanic['Age'] > 10][['Survived', 'Pclass']] 是在使用 Pandas 的 DataFrame 进行行和列的筛选。我们来详细拆解这段代码。

  1. titanic['Age'] > 10: 这部分返回一个布尔型的 Series(也可以看作是一个布尔型的列),其中每个元素表示对应行的 'Age' 值是否大于 10。结果会是一个由 True 和 False 组成的序列。

  2. titanic[titanic['Age'] > 10]: 这部分利用上面生成的布尔型 Series 来筛选出 DataFrame 中 'Age' 大于 10 的所有行。这种筛选方法在 Pandas 中称为布尔索引(Boolean indexing)。

  3. [['Survived', 'Pclass']]: 这部分用于筛选特定的列。当你使用一个列名列表来索引 DataFrame 时,它会返回一个新的 DataFrame,包含你指定的列。

所以,subset_a = titanic[titanic['Age'] > 10][['Survived', 'Pclass']] 整体的意思是:从 titanic 数据集中筛选出 'Age' 大于 10 的行,并仅保留 'Survived' 和 'Pclass' 这两列。

b. 找到年龄最小的 10 个人,并计算他们的年龄的平均值和标准差。

subset_b = titanic.nsmallest(10, 'Age')['Age']
mean_age = subset_b.mean()
std_age = subset_b.std()
解析

这段代码的目标是找到数据集中年龄最小的 10 个人,并计算他们年龄的平均值和标准差。

  1. subset_b = titanic.nsmallest(10, 'Age')['Age']

    • titanic.nsmallest(10, 'Age'): 这个方法从 “titanic” 数据框中选择了年龄最小的 10 个人。nsmallest 方法对数据框中的指定列(在这里是 'Age' 列)进行排序,并返回前 n 个最小值对应的行。在这个例子中,我们想要前 10 个最小的年龄。

    • ['Age']: 这部分代码是对上面返回的 10 行数据进行列子集选择,仅选择 'Age' 列。结果是一个包含 10 个最小年龄的 Pandas 序列。

  2. mean_age = subset_b.mean()

    • subset_b.mean(): 这个方法计算 Pandas 序列 subset_b 的平均值。由于 subset_b 包含了年龄最小的 10 个人的年龄,因此这个方法会返回这 10 个年龄的平均值。
  3. std_age = subset_b.std()

    • subset_b.std(): 这个方法计算 Pandas 序列 subset_b 的标准差。同样地,因为 subset_b 包含了年龄最小的 10 个人的年龄,所以这个方法会返回这 10 个年龄的标准差。

综上所述,这段代码首先找到了年龄最小的 10 个人,然后计算了他们的年龄的平均值和标准差。

4. 排序和计数值

a. 谁支付了最高的票价?

highest_fare_person = titanic[titanic['Fare'] == titanic['Fare'].max()]['Name']
解析

这行代码的目的是找到支付了最高票价的乘客的名字。其实现原理是通过将数据框与其自身的某一列的最大值进行比较,然后利用这种比较来筛选出数据框中的特定行。以下是这行代码的逐步解释:

  1. titanic['Fare'].max():这将从 titanic 数据框的 'Fare' 列中获取最大值,即最高的票价。

  2. titanic['Fare'] == titanic['Fare'].max():这会为 titanic 数据框中的每一行生成一个布尔值(True 或 False),取决于该行的 'Fare' 值是否等于最高票价。如果是,返回 True,否则返回 False。

  3. titanic[titanic['Fare'] == titanic['Fare'].max()]:这将筛选出所有票价等于最高票价的行。这是通过将上述布尔系列作为索引传递给数据框来实现的,这样只有那些其对应布尔值为 True 的行才会被选中。

  4. titanic[titanic['Fare'] == titanic['Fare'].max()]['Name']:最后,这将从筛选出的行中提取 'Name' 列,从而得到支付了最高票价的乘客的名字。

综上所述,这行代码将返回一个系列,其中包含了所有支付了最高票价的乘客的名字。如果只有一名乘客支付了最高票价,那么系列中将只有一个名字;如果有多名乘客支付了相同的最高票价,那么系列中将包含所有这些乘客的名字。

b. 哪个幸存下来的男性支付了最高的票价?

highest_fare_survived_man = titanic[(titanic['Sex'] == 'male') & (titanic['Survived'] == 1)].nlargest(1, 'Fare')['Name']

c. 三等舱里最老的5个人是谁?

oldest_in_class_3 = titanic[titanic['Pclass'] == 3].nlargest(5, 'Age')['Name']

d. 按登船点计算死亡的男性和女性的百分比。

embarked_death_ratio = titanic[titanic['Survived'] == 0].groupby(['Embarked', 'Sex']).size() / titanic.groupby(['Embarked', 'Sex']).size()
  1. 数据清理
    a. 将最老的Mr. Hickman的舱位填充为“B99”。
hickman_index = titanic[titanic['Name'].str.contains('Hickman') & (titanic['Age'] == titanic['Age'].max())].index
titanic.at[hickman_index, 'Cabin'] = 'B99'
  1. 数据转换
    a. 如果行中的Age列有缺失值,则创建一个新列“is_missing_age”。
titanic['is_missing_age'] = np.where(titanic['Age'].isnull(), 1, 0)

b. 使用.apply()创建一个名为“long_name”的新列,该列计算名字中的字母数量,并在超过20时返回“long name”。

titanic['long_name'] = titanic['Name'].apply(lambda x: "long name" if len(x) > 20 else "")

c. 删除新创建的列。

titanic.drop('long_name', axis=1, inplace=True)
  1. 数据分组
    d. 按舱位等级计算平均票价。
avg_fare_by_class = titanic.groupby('Pclass')['Fare'].mean()

e. 平均来说,男性还是女性支付了更多的票价?

avg_fare_by_sex = titanic.groupby('Sex')['Fare'].mean()

f. 创建一个打印输出,显示结果保留到最接近的2位数字。

print(f'Men: {avg_fare_by_sex["male"]:.2f}, Women: {avg_fare_by_sex["female"]:.2f}')

g. 男性的总兄弟姐妹数是多少?女性呢?

total_siblings_by_sex = titanic.groupby('Sex')['SibSp'].sum()
  1. 总结分组数据
    h. 使用.agg()函数查找幸存/未幸存的男性和女性的年龄的平均值和标准差。
agg_data = titanic.groupby(['Sex', 'Survived'])['Age'].agg(['mean', 'std'])
  1. 导出一个只包含完整案例的版本(没有缺失值),命名为'complete_titanic.csv',不带索引号。还剩多少行?
complete_cases = titanic.dropna()
complete_cases.to_csv('complete_titanic.csv', index=False)
rows_remaining = complete_cases.shape[0]

i. 如果要将其导出为Excel文件,并命名工作表为“passenger_list”,应如何操作?

complete_cases.to_excel('complete_titanic.xlsx', sheet_name='passenger_list', index=False)

此实现应该是正确的,但是实际使用时,您可能需要根据您具体的数据和需求进行适当的调整。

import pandas as pd
import numpy as np

# 读取数据
titanic = pd.read_csv('titanic.csv')

# 1. 将PassengerId转为category类型
titanic['PassengerId'] = titanic['PassengerId'].astype('category')

# 2. 创建Family_size特征
titanic['Family_size'] = titanic['SibSp'] + titanic['Parch']

# 3a. 年龄大于10的行的Survived和Pclass
subset_a = titanic.loc[titanic['Age'] > 10, ['Survived', 'Pclass']]

# 3b. 年龄最小的10个人的年龄的均值和标准差
subset_b = titanic.nsmallest(10, 'Age')['Age']
mean_age, std_age = subset_b.mean(), subset_b.std()

# 4a. 付最高票价的人
highest_fare_person = titanic.loc[titanic['Fare'].idxmax(), 'Name']

# 4b. 幸存的男性中付最高票价的人
survived_men = titanic[(titanic['Sex'] == 'male') & (titanic['Survived'] == 1)]
highest_fare_survived_man = survived_men.loc[survived_men['Fare'].idxmax(), 'Name']

# 4c. 三等舱中年纪最大的5个人
oldest_in_class_3 = titanic[titanic['Pclass'] == 3].nlargest(5, 'Age')['Name'].tolist()

# 5a. 更改Mr. Hickman的舱位
titanic.loc[titanic['Name'].str.contains('Hickman') & titanic['Age'].idxmax(), 'Cabin'] = 'B99'

# 6a. 标记缺失年龄
titanic['is_missing_age'] = titanic['Age'].isnull().astype(int)

# 6b. 标记名字长度大于20的乘客
titanic['long_name'] = titanic['Name'].apply(lambda x: "long name" if len(x) > 20 else "")

# 7d. 按舱位等级计算票价均值
avg_fare_by_class = titanic.groupby('Pclass')['Fare'].mean().to_dict()

# 9. 保存没有缺失值的数据
titanic.dropna().to_csv('complete_titanic.csv', index=False)

公众号:AI悦创【二维码】

AI悦创·编程一对一

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh

C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh

方法一:QQopen in new window

方法二:微信:Jiabcdefh

上次编辑于:
贡献者: AndersonHJB
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度