Bin values based on ranges with pandas [duplicate] Bin values based on ranges with pandas [duplicate] numpy numpy

Bin values based on ranges with pandas [duplicate]


In order to bucket your series, you should use the pd.cut() function, like this:

df['bin'] = pd.cut(df['1'], [0, 50, 100,200])         0    1        file         bin0  person1   24     age.csv     (0, 50]1  person2   17     age.csv     (0, 50]2  person3   98     age.csv   (50, 100]3  person4    6     age.csv     (0, 50]4  person2  166  Height.csv  (100, 200]5  person3  125  Height.csv  (100, 200]6  person5  172  Height.csv  (100, 200]

If you want to name the bins yourself, you can use the labels= argument, like this:

df['bin'] = pd.cut(df['1'], [0, 50, 100,200], labels=['0-50', '50-100', '100-200'])         0    1        file      bin0  person1   24     age.csv     0-501  person2   17     age.csv     0-502  person3   98     age.csv   50-1003  person4    6     age.csv     0-504  person2  166  Height.csv  100-2005  person3  125  Height.csv  100-2006  person5  172  Height.csv  100-200