Skip to content Skip to sidebar Skip to footer

Using Conditional Statement To Subtract Scalar From Pandas Df Column Gives Valueerror: The Truth Value Of A Series Is Ambiguous

I'm trying to execute: if df_trades.loc[:, 'CASH'] != 0: df_trades.loc[:, 'CASH'] -= commission and then I get the error. df_trades.loc[:, 'CASH'] is a column of floats. I want to

Solution 1:

Use np.where

commission = -1
df['CASH'] = np.where(df['CASH'] != 0, df['CASH'] + commission , df['CASH'])

or df.where i.e

df['CASH'] = df['CASH'].where(df['CASH'] == 0,df['CASH']+commission)

or df.mask

df['CASH'] = df['CASH'].mask(df['CASH'] != 0 ,df['CASH']+commission)
Date
2011-01-10   -2558.0
2011-01-11       0.0
2011-01-12       0.0
2011-01-13   -2582.0
Name: CASH, dtype: float64
%%timeit
commission = -1
df['CASH'] = np.where(df['CASH'] != 0, df['CASH'] + commission , df['CASH'])
1000 loops, best of 3: 750 µs per loop

%%timeit
df['CASH'].mask(df['CASH'] != 0 ,df['CASH']+commission)
1000 loops, best of 3: 1.45 ms per loop

%%timeit
df['CASH'].where(df['CASH'] == 0,df['CASH']+commission)
1000 loops, best of 3: 1.55 ms per loop

%%timeit
df.loc[df['CASH'] != 0, 'CASH'] += commission
100 loops, best of 3: 2.37 ms per loop

Solution 2:

This should do it:

df.loc[df['CASH'] != 0, 'CASH'] -= 1

Post a Comment for "Using Conditional Statement To Subtract Scalar From Pandas Df Column Gives Valueerror: The Truth Value Of A Series Is Ambiguous"