1

I know that kind of these questions have been raised already, but for my specific problem I couldn't find any answers.

I have a csv with a lot of values in a column 'score'. If one value is greater than 0,001, I want to replace it with 1. If the value is smaller than -0,001, I want to replace it with -1. All values between needs to be removed.

I have tried the following:

import pandas as pd
import numpy as np

df = pd.read_csv('test.csv')

a = np.array(df['score'].values.tolist())
df['text']=np.where(a > 0.001, 1, a).tolist()
df['text']=np.where(a < -0.001, -1, a).tolist()

# Second try
#df['score']=df['score'].apply(lambda x: [-1 if x <= 0.001 else x])
#df['score']=df['score'].apply(lambda x: [1 if x > 0.001 else x])

The output only shows me the second expression, but ignores the first one, in both cases. So at this example, I receive a list where each value < -0.001 = -1, nothing more.

Can anybody help me?

Fabs
  • 43
  • 4
  • Look at `np.select` in the linked answer. – Erfan Jun 27 '19 at 14:45
  • Try: `df['text'] = np.select([df['score'] > 0.001, df['score'] < -0.001], [1, -1], default=np.NaN)` – Erfan Jun 27 '19 at 14:47
  • Your second expression ignores the result of the first, it should have been `df['text']=np.where(df['text'] < -0.001, -1, a).tolist()` PS you don't need to work with lists, np.where is fine with arrays and Series. – cs95 Jun 27 '19 at 14:47
  • Thank you so much! Didn't know how to combine several conditions in one line. – Fabs Jun 27 '19 at 15:05

0 Answers0