w3resource

Pandas Pivot Titanic: Create a Pivot table with multiple indexes from the data set of titanic.csv

Pandas: Pivot Titanic Exercise-3 with Solution

Write a Pandas program to create a Pivot table with multiple indexes from the data set of titanic.csv. Go to Editor

Sample Solution:

Python Code :

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = pd.pivot_table(df, index = ["sex","age"], aggfunc=np.sum)
print(result)

Sample Output:

              Unnamed: 15  adult_male  alone    ...     pclass  sibsp  survived
sex    age                                      ...                            
female 0.75           0.0         0.0    0.0    ...          6      4         2
       1.00           0.0         0.0    0.0    ...          6      1         2
       2.00           0.0         0.0    0.0    ...         15      9         2
       3.00           0.0         0.0    0.0    ...          5      4         1
       4.00           0.0         0.0    0.0    ...         13      4         5
       5.00           0.0         0.0    1.0    ...         11      7         4
       6.00           0.0         0.0    0.0    ...          5      4         1
       7.00           0.0         0.0    0.0    ...          2      0         1
       8.00           0.0         0.0    0.0    ...          5      3         1
       9.00           0.0         0.0    0.0    ...         12     10         0
       10.00          0.0         0.0    0.0    ...          3      0         0
       11.00          0.0         0.0    0.0    ...          3      4         0
       13.00          0.0         0.0    1.0    ...          5      0         2
       14.00          0.0         0.0    1.0    ...          9      3         3
       14.50          0.0         0.0    0.0    ...          3      1         0
       15.00          0.0         0.0    2.0    ...         10      1         4
       16.00          0.0         0.0    3.0    ...         12      5         5
       17.00          0.0         0.0    3.0    ...         12      6         5
       18.00          0.0         0.0    4.0    ...         31      6         8
       19.00          0.0         0.0    3.0    ...         13      3         7
       20.00          0.0         0.0    1.0    ...          6      1         0
       21.00          0.0         0.0    4.0    ...         16      5         4
       22.00          0.0         0.0    7.0    ...         26      3        10
       23.00          0.0         0.0    3.0    ...         10      4         4
       24.00          0.0         0.0    7.0    ...         31     10        14
       25.00          0.0         0.0    1.0    ...         11      3         2
       26.00          0.0         0.0    3.0    ...         12      2         3
       27.00          0.0         0.0    2.0    ...         15      2         5
       28.00          0.0         0.0    4.0    ...         16      3         5
       29.00          0.0         0.0    2.0    ...         16      3         5
...                   ...         ...    ...    ...        ...    ...       ...
male   42.00          0.0        10.0    6.0    ...         21      3         3
       43.00          0.0         3.0    2.0    ...          8      1         0
       44.00          0.0         6.0    3.0    ...         15      3         1
       45.00          0.0         6.0    5.0    ...         10      1         2
       45.50          0.0         2.0    2.0    ...          4      0         0
       46.00          0.0         3.0    2.0    ...          4      1         0
       47.00          0.0         7.0    7.0    ...         12      0         0
       48.00          0.0         5.0    3.0    ...          8      2         3
       49.00          0.0         4.0    1.0    ...          6      3         2
       50.00          0.0         5.0    2.0    ...          8      4         1
       51.00          0.0         6.0    5.0    ...         13      0         1
       52.00          0.0         4.0    3.0    ...          6      1         1
       54.00          0.0         5.0    3.0    ...          8      1         0
       55.00          0.0         1.0    1.0    ...          1      0         0
       55.50          0.0         1.0    1.0    ...          3      0         0
       56.00          0.0         3.0    3.0    ...          3      0         1
       57.00          0.0         1.0    1.0    ...          2      0         0
       58.00          0.0         2.0    1.0    ...          2      0         0
       59.00          0.0         2.0    2.0    ...          5      0         0
       60.00          0.0         3.0    1.0    ...          4      2         1
       61.00          0.0         3.0    3.0    ...          5      0         0
       62.00          0.0         3.0    3.0    ...          4      0         1
       64.00          0.0         2.0    1.0    ...          2      1         0
       65.00          0.0         3.0    2.0    ...          5      0         0
       66.00          0.0         1.0    1.0    ...          2      0         0
       70.00          0.0         2.0    1.0    ...          3      1         0
       70.50          0.0         1.0    1.0    ...          3      0         0
       71.00          0.0         2.0    2.0    ...          2      0         0
       74.00          0.0         1.0    1.0    ...          3      0         0
       80.00          0.0         1.0    1.0    ...          1      0         1

[145 rows x 8 columns]                                    

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 extract the column labels, shape and data types of the dataset (titanic.csv)
Next: Write a Pandas program to create a Pivot table and find survival rate by gender on various classes.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/pandas/excel/pandas-pivot-titanic-exercise-3.php