Using an example df
and @Pygirl's idea:
df = pd.DataFrame({'a': np.random.randint(0, 400, 10), 'b': np.random.randint(0, 400, 10), 'X': np.zeros(10)})
gives us:
| | a | b | X |
|---:|----:|----:|----:|
| 0 | 237 | 188 | 0 |
| 1 | 212 | 147 | 0 |
| 2 | 135 | 30 | 0 |
| 3 | 296 | 154 | 0 |
| 4 | 133 | 219 | 0 |
| 5 | 185 | 317 | 0 |
| 6 | 365 | 5 | 0 |
| 7 | 108 | 189 | 0 |
| 8 | 358 | 34 | 0 |
| 9 | 105 | 2 | 0 |
with pd.cut
doc we can get:
df['X'] = pd.cut(df.a, [0, 100, 200, 300, np.inf], labels=[1, 2, 3, 4])
which results in:
| | a | b | X |
|---:|----:|----:|----:|
| 0 | 377 | 232 | 4 |
| 1 | 61 | 11 | 1 |
| 2 | 52 | 217 | 1 |
| 3 | 191 | 42 | 2 |
| 4 | 178 | 228 | 2 |
| 5 | 235 | 206 | 3 |
| 6 | 39 | 222 | 1 |
| 7 | 316 | 210 | 4 |
| 8 | 135 | 390 | 2 |
| 9 | 44 | 311 | 1 |