w3resource

Pandas Pivot Titanic: Find survival rate by gender on various classes


4. Pivot Table: Survival Rate by Gender on Various Classes

Write a Pandas program to create a Pivot table and find survival rate by gender on various classes. 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('survived', index='sex', columns='class')
print(result)

Sample Output:

class      First    Second     Third
sex                                 
female  0.968085  0.921053  0.500000
male    0.368852  0.157407  0.135447	                                       

For more Practice: Solve these Related Problems:

  • Write a Pandas program to create a pivot table from titanic.csv that computes the survival rate for each class, split by gender.
  • Write a Pandas program to generate a pivot table that shows the proportion of survivors by gender for each passenger class.
  • Write a Pandas program to build a pivot table that calculates survival percentages for each combination of class and gender.
  • Write a Pandas program to create a pivot table from titanic.csv and compare survival rates by gender across different classes using normalized values.

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 with multiple indexes from the data set of titanic.csv.
Next: Write a Pandas program to create a Pivot table and find survival rate by gender.

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.