Pandas Pivot Titanic: Separate the gender according to whether they traveled alone or not to get the probability of survival
18. Pivot Table: Survival by Solo Boarding
Write a Pandas program to create a Pivot table and separate the gender according to whether they traveled alone or not to get the probability of survival. 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' , [ 'sex' , 'alone' ] , 'class' )
print(result)
Sample Output:
class First Second Third sex alone female False 0.966667 0.931818 0.416667 True 0.970588 0.906250 0.616667 male False 0.425532 0.277778 0.180723 True 0.333333 0.097222 0.121212
For more Practice: Solve these Related Problems:
- Write a Pandas program to create a pivot table that separates passengers by whether they traveled alone or not, then computes survival rates by gender.
- Write a Pandas program to generate a pivot table that shows survival probability based on solo boarding status and gender.
- Write a Pandas program to build a pivot table from titanic.csv that groups passengers by solo versus accompanied and calculates survival percentages.
- Write a Pandas program to create a pivot table that partitions passengers by solo boarding and then cross-tabulates survival with gender.
Go to:
Previous: Write a Pandas program to create a Pivot table and gender wise sum of survival and cabin class wise mean value of fare.
Next: Write a Pandas program to create a Pivot table and find the probability of survival by class, gender, solo boarding and port of embarkation.
Python Code Editor:
Pivot Titanic.csv:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.