0

The data frame contains 5 columns named V, W, X, Y, Z.

I'm supposed to change the values in column X from a dataset according to:

  • if 1 to 100, change to 1
  • if 101 to 200, change to 2
  • if 201 to 300, change to 3

otherwise, change to 4

What's the most efficient way this can be done?

1 Answers1

0

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 |
Albo
  • 1,584
  • 9
  • 27