2

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 subtract the scalar commission from each entry in that column.

For example, df_trades.loc[:, 'CASH'] prints out

2011-01-10   -2557.0000
2011-01-11       0.0000
2011-01-12       0.0000
2011-01-13   -2581.0000

If commission is 1, I want the result:

2011-01-10   -2558.0000
2011-01-11       0.0000
2011-01-12       0.0000
2011-01-13   -2582.0000

2 Answers2

2

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
Bharath M Shetty
  • 30,075
  • 6
  • 57
  • 108
0

This should do it:

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

elPastor
  • 8,435
  • 11
  • 53
  • 81
  • Is this the most Pythonic solution given I'm using Pandas and Numpy? –  Oct 19 '17 at 13:05
  • I'm not the arbiter on pythonic code, but it works and is clean. Nor have I tested the speed, so give them both a shot and see what works best for you. – elPastor Oct 19 '17 at 13:07