1

I have a datarframe

                          dt       id  ...   timestamp prediction
0  2021-04-12 03:17:25+00:00  gu7thj8  ...  2021-04-12   0.386086
1  2021-04-12 03:17:26+00:00  gu7thjl  ...  2021-04-12   0.708179
2  2021-04-12 03:17:28+00:00  gu7thoz  ...  2021-04-12   0.410866
5  2021-04-12 14:22:04+00:00  gu99dqg  ...  2021-04-12   0.416667

if the number between 0 and 0.40 return -1 if the number between 0.40 and 0.60 return 0 if the number between 0.60 and 1 return 1 and I want to create an extra column that would give an output with -1,0,1

YanRemes
  • 347
  • 2
  • 10
  • what do you mean by 'the number'? Is this your prediction column? – Emi OB Sep 08 '21 at 12:34
  • so i am looking at the prediction column and if lets say 0.386086 between 0 and 0.40 -1 will occur in the new column at this row – YanRemes Sep 08 '21 at 12:36

1 Answers1

2

Use np.select:

df['new'] = np.select([df['prediction'].between(0, 0.4, inclusive='left'),
                       df['prediction'].between(0.4, 0.6, inclusive='left'),
                       df['prediction'].between(0.6, 1)],
                      choicelist=[-1, 0, 1])

Output:

>>> df
                         dt       id  timestamp  prediction  new
0 2021-04-12 03:17:25+00:00  gu7thj8 2021-04-12    0.386086   -1
1 2021-04-12 03:17:26+00:00  gu7thjl 2021-04-12    0.708179    1
2 2021-04-12 03:17:28+00:00  gu7thoz 2021-04-12    0.410866    0
5 2021-04-12 14:22:04+00:00  gu99dqg 2021-04-12    0.416667    0
Corralien
  • 109,409
  • 8
  • 28
  • 52