w3resource

Pandas Pivot Titanic: Find survival rate by gender


5. Pivot Table: Survival Rate by Gender

Write a Pandas program to create a Pivot table and find survival rate by gender. Go to Editor

Sample Solution:

Python Code :

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result=df.groupby('sex')[['survived']].mean()
print(result)

Sample Output:

	        survived
sex             
female  0.742038
male    0.188908                                       

For more Practice: Solve these Related Problems:

  • Write a Pandas program to create a pivot table that calculates the overall survival rate for each gender in titanic.csv.
  • Write a Pandas program to generate a pivot table that aggregates survival counts by gender and computes the corresponding survival rate.
  • Write a Pandas program to build a pivot table from titanic.csv displaying survival rates solely by gender.
  • Write a Pandas program to create a pivot table that normalizes the survival data by gender and outputs the resulting percentages.

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 survival rate by gender on various classes.
Next: Write a Pandas program to create a Pivot table and find survival rate by gender, age wise of various classes.

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.