Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

create age-groups in pandas

X_train_data = pd.DataFrame({'Age':[0,2,4,13,35,-1,54]})

bins= [0,2,4,13,20,110]
labels = ['Infant','Toddler','Kid','Teen','Adult']
X_train_data['AgeGroup'] = pd.cut(X_train_data['Age'], bins=bins, labels=labels, right=False)
print (X_train_data)
   Age AgeGroup
0    0   Infant
1    2  Toddler
2    4      Kid
3   13     Teen
4   35    Adult
5   -1      NaN
6   54    Adult
Comment

PREVIOUS NEXT
Code Example
Python :: pandas add column with constant value 
Python :: python join with int 
Python :: pre commit python 
Python :: unzip_data python 
Python :: convert data type object to string python 
Python :: numpy how to slice individual columns 
Python :: sort dictionary by value python 
Python :: combine two dictionary adding values for common keys 
Python :: what is the use of class in python 
Python :: when was python developed 
Python :: python change terminal name 
Python :: python get pid of process 
Python :: dataframe pandas to spark 
Python :: how to make a numpy array 
Python :: s = 1 + 2 + ... + n in python 
Python :: outliers removal pandas 
Python :: python pair two lists into a dictionary 
Python :: mutable and immutable in python 
Python :: take the first in dataloader pytorch 
Python :: convert pandas dataframe/ table to python dictionary 
Python :: pipenv with specific python version 
Python :: how to slice even index value from a list in python using slice function 
Python :: sqlite check if table exists 
Python :: genrate unique key in python 
Python :: define empty numpy array 
Python :: where are python libraries installed in windows 
Python :: how to read numbers from a text file in python 
Python :: python get item from queue 
Python :: view(-1) in pytorch 
Python :: how to fetch all chars of a string before a space in python 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =