使用条件语句从pandas df列中减去标量会产生ValueError:系列的真值是模糊的

时间:2017-10-19 12:52:25

标签: python pandas scalar truthiness

我试图执行:

if df_trades.loc[:, 'CASH'] != 0: df_trades.loc[:, 'CASH'] -= commission

然后我收到错误。 df_trades.loc[:, 'CASH']是一列浮点数。我想从该列中的每个条目中减去标量commission

例如,df_trades.loc[:, 'CASH']打印出来

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

如果commission1,我想要结果:

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

2 个答案:

答案 0 :(得分:3)

使用np.where

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

df.where

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

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

答案 1 :(得分:1)

这应该这样做:

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