Example 1: dataframe cut based on range
test = pd.DataFrame({'days': [0,20,30,31,45,60]})
test['range1'] = pd.cut(test.days, [0,30,60], include_lowest=True)
test['range2'] = pd.cut(test.days, [0,30,60], right=False)
test['range3'] = pd.cut(test.days, [0,30,60])
print (test)
days range1 range2 range3
0 0 (-0.001, 30.0] [0, 30) NaN
1 20 (-0.001, 30.0] [0, 30) (0, 30]
2 30 (-0.001, 30.0] [30, 60) (0, 30]
3 31 (30.0, 60.0] [30, 60) (30, 60]
4 45 (30.0, 60.0] [30, 60) (30, 60]
5 60 (30.0, 60.0] NaN (30, 60]
Example 2: pd.cut in pandas
>>> pd.qcut(range(5), 4, labels=False)
array([0, 0, 1, 2, 3])
Example 3: pandas bins dummy
>>> dout
age ageD_[5, 30] ageD_(30, 70] (-inf, 5] (-inf, 30] (-inf, 70] (5, +inf) (30, +inf) (70, +inf)
0 5 1 0 1 1 1 0 0 0
1 23 1 0 0 1 1 1 0 0
2 43 0 1 0 0 1 1 1 0
3 70 0 1 0 0 1 1 1 0
4 30 1 0 0 1 1 1 0 0
Example 4: pd.cut in pandas
>>> pd.qcut(range(5), 3, labels=["good", "medium", "bad"])
...
[good, good, medium, bad, bad]
Categories (3, object): [good < medium < bad]