Home > other >  Change value sign based on another column value
Change value sign based on another column value

Time:03-17

I want to change the sign of column sum by another column value apple. In other words, whenever it sees apple, the sum is in negative value. I thought I could do np.where((ef.fruit == 'apple'), (-1 * ef.sum), ef.sum) but it gave me an error. How can I overcome this?

import pandas as pd
data = {'id':["ab3e3", "psdds2", "pas13", "ccdf2", "dsda1"],
        'fruit':["apple", "others", "organge", "watermelon", "apple"],
        'sum':[2, 3, 6, 8, 3]}

ef = pd.DataFrame(data)
ef

       id       fruit  sum
0   ab3e3       apple    2
1  psdds2      others    3
2   pas13     organge    6
3   ccdf2  watermelon    8
4   dsda1       apple    3

CodePudding user response:

Do not name your column with panda original function, if you do call it via [] not .

np.where((ef.fruit == 'apple'), (-1 * ef['sum']), ef['sum'])
Out[159]: array([-2,  3,  6,  8, -3], dtype=int64)

CodePudding user response:

A simple way to do it would be to make a mask where apple corresponds to -1 and the other values correspond to 1 and then multiple:

df['sum'] *= np.where(df['fruit'].eq('apple'), -1, 1)

Output:

>>> df
       id       fruit  sum
0   ab3e3       apple   -2
1  psdds2      others    3
2   pas13     organge    6
3   ccdf2  watermelon    8
4   dsda1       apple   -3

Another solution would be to use .loc:

df.loc[df['fruit'] == 'apple', 'sum'] = -df['sum']

CodePudding user response:

Using numpy.where:

ef['sum'] *= np.where(ef['fruit'].eq('apple'), -1, 1)

or with pandas loc indexing:

ef.loc[ef['fruit'].eq('apple'), 'sum'] *= -1

output:

       id       fruit  sum
0   ab3e3       apple   -2
1  psdds2      others    3
2   pas13     organge    6
3   ccdf2  watermelon    8
4   dsda1       apple   -3

CodePudding user response:

ef.loc[ef["fruit"]=="apple", "sum"] *= -1

result:

       id       fruit  sum
0   ab3e3       apple   -2
1  psdds2      others    3
2   pas13     organge    6
3   ccdf2  watermelon    8
4   dsda1       apple   -3
  • Related