0% found this document useful (0 votes)
16 views6 pages

Week 4 exercises-SOLN

Uploaded by

zyl178377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Week 4 exercises-SOLN

Uploaded by

zyl178377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Week 4 Exercises

%pylab inline
import seaborn as sns
df = sns.load_dataset("titanic")
df.head(5)

Populating the interactive namespace from numpy and matplotlib

survived pclass sex age sibsp parch fare embarked


class \
0 0 3 male 22.0 1 0 7.2500 S
Third
1 1 1 female 38.0 1 0 71.2833 C
First
2 1 3 female 26.0 0 0 7.9250 S
Third
3 1 1 female 35.0 1 0 53.1000 S
First
4 0 3 male 35.0 0 0 8.0500 S
Third

who adult_male deck embark_town alive alone


0 man True NaN Southampton no False
1 woman False C Cherbourg yes False
2 woman False NaN Southampton yes True
3 woman False C Southampton yes False
4 man True NaN Southampton no True

Exercise 1
• Create a DataFrame with records of all males.

• Check: The DataFrame should consist of 577 rows.

df1 = df[df['sex']=='male']
df1

survived pclass sex age sibsp parch fare embarked


class \
0 0 3 male 22.0 1 0 7.2500 S
Third
4 0 3 male 35.0 0 0 8.0500 S
Third
5 0 3 male NaN 0 0 8.4583 Q
Third
6 0 1 male 54.0 0 0 51.8625 S
First
7 0 3 male 2.0 3 1 21.0750 S
Third
.. ... ... ... ... ... ... ... ...
...
883 0 2 male 28.0 0 0 10.5000 S
Second
884 0 3 male 25.0 0 0 7.0500 S
Third
886 0 2 male 27.0 0 0 13.0000 S
Second
889 1 1 male 26.0 0 0 30.0000 C
First
890 0 3 male 32.0 0 0 7.7500 Q
Third

who adult_male deck embark_town alive alone


0 man True NaN Southampton no False
4 man True NaN Southampton no True
5 man True NaN Queenstown no True
6 man True E Southampton no True
7 child False NaN Southampton no False
.. ... ... ... ... ... ...
883 man True NaN Southampton no True
884 man True NaN Southampton no True
886 man True NaN Southampton no True
889 man True C Cherbourg yes True
890 man True NaN Queenstown no True

[577 rows x 15 columns]

Exercise 2
• Create a DataFrame with records of all males who survived the sinking of Titanic.

• Check: The DataFrame should consist of 109 rows.

df2 = df[(df['sex']=='male')&df['survived'] >0]


df2

survived pclass sex age sibsp parch fare embarked


class \
17 1 2 male NaN 0 0 13.0000 S
Second
21 1 2 male 34.0 0 0 13.0000 S
Second
23 1 1 male 28.0 0 0 35.5000 S
First
36 1 3 male NaN 0 0 7.2292 C
Third
55 1 1 male NaN 0 0 35.5000 S
First
.. ... ... ... ... ... ... ... ...
...
838 1 3 male 32.0 0 0 56.4958 S
Third
839 1 1 male NaN 0 0 29.7000 C
First
857 1 1 male 51.0 0 0 26.5500 S
First
869 1 3 male 4.0 1 1 11.1333 S
Third
889 1 1 male 26.0 0 0 30.0000 C
First

who adult_male deck embark_town alive alone


17 man True NaN Southampton yes True
21 man True D Southampton yes True
23 man True A Southampton yes True
36 man True NaN Cherbourg yes True
55 man True C Southampton yes True
.. ... ... ... ... ... ...
838 man True NaN Southampton yes True
839 man True C Cherbourg yes True
857 man True E Southampton yes True
869 child False NaN Southampton yes False
889 man True C Cherbourg yes True

[109 rows x 15 columns]

Exercise 3
• Find the age of the oldest Titanic passenger.

• Check: You should get 80.

max(df['age'])

80.0

Exercise 4
• Get the record of the oldest passenger.

• Check: The passenger was a male, embarked in Southampton, traveled in the first
class and survived.
df[df['age']==max(df['age'])]

survived pclass sex age sibsp parch fare embarked class


who \
630 1 1 male 80.0 0 0 30.0 S First
man

adult_male deck embark_town alive alone


630 True A Southampton yes True

Exercise 5
• Create a DataFrame with records of the 5 oldest females.

• Check: The ages of the women should be: 63, 63, 62, 60, and 58.

df5 = df[(df['sex']=='female' )].sort_values(by='age',


ascending=False)[:5]
df5

survived pclass sex age sibsp parch fare embarked


class \
483 1 3 female 63.0 0 0 9.5875 S
Third
275 1 1 female 63.0 1 0 77.9583 S
First
829 1 1 female 62.0 0 0 80.0000 NaN
First
366 1 1 female 60.0 1 0 75.2500 C
First
268 1 1 female 58.0 0 1 153.4625 S
First

who adult_male deck embark_town alive alone


483 woman False NaN Southampton yes True
275 woman False D Southampton yes False
829 woman False B NaN yes True
366 woman False D Cherbourg yes False
268 woman False C Southampton yes False

Exercise 6
• Find the record of the oldest female who did not survive.

• Check: She was 57, embarked in Southampton and traveled in the second class.
df[(df['sex']=='female' ) & (df['survived']==0)].sort_values(by='age',
ascending=False)[:1]

survived pclass sex age sibsp parch fare embarked


class \
772 0 2 female 57.0 0 0 10.5 S
Second

who adult_male deck embark_town alive alone


772 woman False E Southampton no True

Exercise 7
• Find the number of people who survived the sinking and the number of people who
died.

• Check: 342 survived, 549 died.

print(df['survived'].sum(),len(df['survived']) - df['survived'].sum())

342 549

Exercise 8
• What was the average age of people who survived?

• Check: 28.34 years.

df[df['survived']==1]['age'].mean()

28.343689655172415

Exercise 9
• There were three classes of passengers aboard Titanic: “First”, “Second” and “Third”.
Compute what fraction of passengers traveling in each class survived.

• Check: First: 0.629, Second: 0.473, Third: 0.242.

class_groups = df.groupby('class')
class_groups['survived'].sum() / class_groups['survived'].count()
class
First 0.629630
Second 0.472826
Third 0.242363
Name: survived, dtype: float64

You might also like