Bin values based on ranges with pandas [duplicate]
Solution 1:
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 bin
0 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 bin
0 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