MasoodQaim
MasoodQaim

Reputation: 47

Convert values to negative on the condition of values from another column

I have a df where I want to convert the value in column b to a negative number if the corresponding value in column a is not NaN.

Here is my code:

# create a test df
df = pd.DataFrame(np.array([[12, 34], [67, 98], [np.nan, 6], [23, 45], [np.nan, 5], [5, 82], [61, 92]]), columns=['a', 'b'])

print(df)
    a   b
0   12.0    34.0
1   67.0    98.0
2   NaN     6.0
3   23.0    45.0
4   NaN     5.0


# the code to convert b to negative if a is not NaN
df['b'] = df['b'].apply(lambda x: -x if df['a'] != pd.isnull else x)

This is error is receive:

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

I tried to look up the error code but I don't understand what is happening. I am using lambda but not sure if there is a better approach. The df should look like this:


    a   b
0   12.0    -34.0
1   67.0    -98.0
2   NaN     6.0
3   23.0    -45.0
4   NaN     5.0

Upvotes: 0

Views: 2335

Answers (2)

BallpointBen
BallpointBen

Reputation: 13750

A simple one-liner gives you the column you desire:

df['b'].where(df['a'].isna(), -df['b'])

Or if you want to modify df in place:

df.loc[df['a'].isna(), 'b'] *= -1

Upvotes: 2

wasif
wasif

Reputation: 15478

I will use combine_first with dropna, may RAW attempt ;-)

import numpy as np
import pandas as pd
df = pd.DataFrame(np.array([[12, 34], [67, 98], [np.nan, 6], [23, 45], [np.nan, 5], [5, 82], [61, 92]]), columns=['a', 'b'])
df['b'] = df.dropna()['b'].apply(lambda x: -x).combine_first(df['b'])
print(df)

Output

a     b
0  12.0 -34.0
1  67.0 -98.0
2   NaN   6.0
3  23.0 -45.0
4   NaN   5.0
5   5.0 -82.0
6  61.0 -92.0

Upvotes: 1

Related Questions