IIUC you could use cut to achieve this:
In[33]:
pd.cut(df['a'], bins=[0,3,7,11], right=True, labels=False)+1
Out[33]:
0 2
1 3
2 3
3 1
4 1
5 1
6 1
7 3
8 2
9 2
Here you'd pass the cutoff values to cut
, and this will categorise your values, by passing labels=False
it will give them an ordinal value (zero-based) so you just +1
to them
Here you can see how the cuts were calculated:
In[34]:
pd.cut(df['a'], bins=[0,3,7,11], right=True)
Out[34]:
0 (3, 7]
1 (7, 11]
2 (7, 11]
3 (0, 3]
4 (0, 3]
5 (0, 3]
6 (0, 3]
7 (7, 11]
8 (3, 7]
9 (3, 7]
Name: a, dtype: category
Categories (3, interval[int64]): [(0, 3] < (3, 7] < (7, 11]]