w3resource

Pandas Pivot Titanic: Find number of adult male, adult female and children

Pandas: Pivot Titanic Exercise-16 with Solution

Write a Pandas program to create a Pivot table and find number of adult male, adult female and children. Go to Editor

Sample Solution:

Python Code :

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = df.pivot_table('sex',  'who', aggfunc = 'count')
print(result)

Sample Output:

       sex
who       
child   83
man    537
woman  271        

Python Code Editor:


Pivot Titanic.csv:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to create a Pivot table and find number of survivors and average rate grouped by gender and class.
Next: Write a Pandas program to create a Pivot table and check missing values of children.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.