Home > Back-end >  Replacing positive, negative, and zero values by 1, -1, and 0 respectively
Replacing positive, negative, and zero values by 1, -1, and 0 respectively

Time:04-18

I have a pandas dataframe(100,000 obs) with 11 columns.

I'm trying to assign df['trade_sign'] values based on the df['diff'] (which is a pd.series object of integer values)

If diff is positive, then trade_sign = 1
if diff is negative, then trade_sign = -1
if diff is 0, then trade_sign = 0

What I've tried so far:

pos['trade_sign'] = (pos['trade_sign']>0) <br>
pos['trade_sign'].replace({False: -1, True: 1}, inplace=True)

But this obviously doesn't take into account 0 values.
I also tried for loops with if conditions but that didn't work.

Essentially, how do I fix my .replace function to take account of diff values of 0.

Ideally, I'd prefer a solution that uses numpy over for loops with if conditions.

dataframe

CodePudding user response:

There's a sign function in numpy:

df["trade_sign"] = np.sign(df["diff"])

If you want integers,

df["trade_sign"] = np.sign(df["diff"]).astype(int)

CodePudding user response:

    a = [-1 if df['diff'].values[i] < 0 else 1 for i in range(len(df['diff'].values))]
    df['trade_sign'] = a

CodePudding user response:

You could do it this way:

pos['trade_sign'] = (pos['diff'] > 0) * 1   (pos['diff'] < 0) * -1

The boolean results of the element-wise > and < comparisons automatically get converted to int in order to allow multiplication with 1 and -1, respectively.

This sample input and test code:

import pandas as pd
pos = pd.DataFrame({'diff':[-9,0,9,-8,0,8,-7-6-5,4,3,2,0]})
pos['trade_sign'] = (pos['diff'] > 0) * 1   (pos['diff'] < 0) * -1
print(pos)

... gives this output:

    diff  trade_sign
0     -9          -1
1      0           0
2      9           1
3     -8          -1
4      0           0
5      8           1
6    -18          -1
7      4           1
8      3           1
9      2           1
10     0           0

UPDATE: In addition to the solution above, as well as some of the other excellent ideas in other answers, you can use numpy where:

pos['trade_sign'] = np.where(pos['diff'] > 0, 1, np.where(pos['diff'] < 0, -1, 0))
  • Related