Pandas Pivot Titanic: Find number of adult male, adult female and children
16. Pivot Table: Count Adult Males, Adult Females, and Children
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
For more Practice: Solve these Related Problems:
- Write a Pandas program to partition passengers into adult males, adult females, and children, then create a pivot table counting each category.
- Write a Pandas program to generate a pivot table from titanic.csv that categorizes passengers by age and gender into adult and child groups.
- Write a Pandas program to build a pivot table that displays counts for adult males, adult females, and children using custom age thresholds.
- Write a Pandas program to create a pivot table that cross-tabulates passenger groups into adult male, adult female, and child categories.
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.