Using Pandas
Using the titanic dataset from seaborn with modifications shown below, write the Pandas code to show the results for the following questions. The code should be as elegant as possible. Do not use any loops. No plots are required.
a) How many males and females are there on the ship? How many children are there on the ship based on the who attribute? Among the children, how many of them are male and female?
b) Based on the gender/sex, what is the average age of the survivors in each group?
c) Using the age range (0,18] as Child and (18,80] as Adult derived from the age attribute, show the number of survivors based on gender (sex), age range, and traveling class (class).
[2]: titanic = sns.load_dataset('titanic')
titanic
t[2]:
sex class who survived
male 22.0 Third man First woman Third woman First woman
0 1 1 1 0
1 female 38.0
2 female 26.0 female 35.0
4
male 35.0 Third
man
..
-..
-.
-.
886 male 27.0 Second
man
0
887 female 19.0
First woman
1
888 female NaN
Third woman
0
688 male 26.0 890 male 32.0
First Third
man
1 0
man
891 rows x 5 columns