Reputation: 679
I have a data frame that depending on the value of column Order
I want to take the value of column Value
and do some calculation.
DataFrame1
Order Shares Value
2011-01-10 BUY 1300 340.99
2011-01-10 SELL 1200 340.99
2011-01-11 SELL 1100 330.99
Line of code:
impacts['NewValue']=float(impacts.Order.apply(lambda x: (impacts.Value + (impacts.Value * 0.006)) if x == 'SELL' else (impacts.Value - (impacts.Value * 0.006))))
Error:
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')
Is my understanding that error is caused by the contents of the numbers, therefore that's why I tried to cast it to a float.
Intended output
Order Shares Value NewValue
2011-01-10 BUY 1300 340.99 338.94
2011-01-10 SELL 1200 340.99 343.03
2011-01-11 SELL 1100 330.99 332.97
Any help is more than welcome. Thanks!
Upvotes: 2
Views: 6313
Reputation: 402263
(Too long for a comment) Here's a slightly more condensed version of Wen's np.where
:
i = np.where(df.Order == 'SELL', 1, -1) * 0.006
df.Value = df.Value.mul(i) + df.Value
print(df.Value)
2011-01-10 338.94406
2011-01-10 343.03594
2011-01-11 332.97594
dtype: float64
Use df.Order
to determine the sign before the operation.
Upvotes: 2
Reputation: 323226
Hope it help:-) (Modified your own code only, Your sample code will return error)
df.apply(lambda x: (x.Value + (x.Value * 0.006)) if x.Order == 'SELL' else (x.Value - (x.Value * 0.006)),axis=1)
Out[790]:
2011-01-10 338.94406
2011-01-10 343.03594
2011-01-11 332.97594
dtype: float64
To get df
df['NewValue']=df.apply(lambda x: (x.Value + (x.Value * 0.006)) if x.Order == 'SELL' else (x.Value - (x.Value * 0.006)),axis=1)
df
Out[792]:
Order Shares Value NewValue
2011-01-10 BUY 1300 340.99 338.94406
2011-01-10 SELL 1200 340.99 343.03594
2011-01-11 SELL 1100 330.99 332.97594
I will use np.where
import numpy as np
np.where(df.Order=='SELL',(df.Value + (df.Value * 0.006)),(df.Value - (df.Value * 0.006)) )
Out[794]: array([ 338.94406, 343.03594, 332.97594])
After assign it back
df['NewValue']=np.where(df.Order=='SELL',(df.Value + (df.Value * 0.006)),(df.Value - (df.Value * 0.006)) )
df
Out[796]:
Order Shares Value NewValue
2011-01-10 BUY 1300 340.99 338.94406
2011-01-10 SELL 1200 340.99 343.03594
2011-01-11 SELL 1100 330.99 332.97594
Upvotes: 2