Week 4 exercises-SOLN
Week 4 exercises-SOLN
%pylab inline
import seaborn as sns
df = sns.load_dataset("titanic")
df.head(5)
Exercise 1
• Create a DataFrame with records of all males.
df1 = df[df['sex']=='male']
df1
Exercise 2
• Create a DataFrame with records of all males who survived the sinking of Titanic.
Exercise 3
• Find the age of the oldest Titanic passenger.
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'])]
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.
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]
Exercise 7
• Find the number of people who survived the sinking and the number of people who
died.
print(df['survived'].sum(),len(df['survived']) - df['survived'].sum())
342 549
Exercise 8
• What was the average age of people who survived?
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.
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