January 2018
Beginner to intermediate
316 pages
7h 14m
English
Sometimes, when you have continuous numerical data, it may make sense to transform a continuous variable into a categorical variable. For example, say you have ages, but it would be more useful to work with age ranges.
pandas has a useful function called cut that will bin your data for you. By binning, we mean it will create the ranges for your data.
Let's see how this function could work on our quantitative_column:
# name of category is the bin by default
pd.cut(X['quantitative_column'], bins=3)
The output of the cut function for our quantitative column looks like this:
0 (-0.52, 6.333] 1 (6.333, 13.167] 2 (-0.52, 6.333] 3 (6.333, 13.167] 4 NaN 5 (13.167, 20.0] Name: quantitative_column, ...
Read now
Unlock full access