2

I have a dataframe with several columns and indexes and I would like to replace each value by 1 if the value is positive, -1 else.
Here is how I tried to do it but it fails and I have th efollowing error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

There are many other posts with the same error but none of the one I read helped.

Here is my code:

        numeric_cols = [col for col in df1 if df1[col].dtype.kind != 'O']
        if df1[numeric_cols] > 0:
            df1[numeric_cols] = 1
        else:
            df1[numeric_cols] = -1
astudentofmaths
  • 1,122
  • 2
  • 19
  • 33

2 Answers2

4

.apply is going to be very slow. You're betting off just doing two operations:

df = pd.DataFrame([[1,2,3],[1,5,2],[2,3,1]])
print df

   0  1  2
0  1  2  3
1  1  5  2
2  2  3  1    

df[df<3]=0
df[df>=3]=1

print df

   0  1  2
0  0  0  1
1  0  1  0
2  0  1  0
flyingmeatball
  • 7,457
  • 7
  • 44
  • 62
0

df1[numeric_cols] gives you the numeric_cols of all the rows in your dataset, and so you're not looking at the data row by row.

df1[numeric_cols] = df1[numeric_cols].apply(lambda x : 1 if x > 0 else -1) 

should work.

Similar to Replace an entry in a pandas DataFrame using a conditional statement

Community
  • 1
  • 1
yujia21
  • 114
  • 5