Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Pandas Pivot Titanic: Partition each of the passengers into four categories based on their age

Pandas: Pivot Titanic Exercise-7 with Solution

Write a Pandas program to partition each of the passengers into four categories based on their age. Go to Editor

Note: Age categories (0, 10), (10, 30), (30, 60), (60, 80)

Sample Solution:

Python Code :

import pandas as pd
import numpy as np
df = pd.read_csv('titanic.csv')
result = pd.cut(df['age'], [0, 10, 30, 60, 80])
print(result)

Sample Output:

0      (10, 30]
1      (30, 60]
2      (10, 30]
3      (30, 60]
4      (30, 60]
5           NaN
6      (30, 60]
7       (0, 10]
8      (10, 30]
9      (10, 30]
10      (0, 10]
11     (30, 60]
12     (10, 30]
13     (30, 60]
14     (10, 30]
15     (30, 60]
16      (0, 10]
17          NaN
18     (30, 60]
19          NaN
20     (30, 60]
21     (30, 60]
22     (10, 30]
23     (10, 30]
24      (0, 10]
25     (30, 60]
26          NaN
27     (10, 30]
28          NaN
29          NaN
         ...   
861    (10, 30]
862    (30, 60]
863         NaN
864    (10, 30]
865    (30, 60]
866    (10, 30]
867    (30, 60]
868         NaN
869     (0, 10]
870    (10, 30]
871    (30, 60]
872    (30, 60]
873    (30, 60]
874    (10, 30]
875    (10, 30]
876    (10, 30]
877    (10, 30]
878         NaN
879    (30, 60]
880    (10, 30]
881    (30, 60]
882    (10, 30]
883    (10, 30]
884    (10, 30]
885    (30, 60]
886    (10, 30]
887    (10, 30]
888         NaN
889    (10, 30]
890    (30, 60]
Name: age, Length: 891, dtype: category
Categories (4, interval[int64]): [(0, 10] < (10, 30] < (30, 60] < (60, 80]]                

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m