根据 Pandas 上的列值应用 lambda
Apply lambda depending on column values on Pandas
我有一个数据框,它取决于列 Order
的值我想获取列 Value
的值并进行一些计算。
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
代码行:
impacts['NewValue']=float(impacts.Order.apply(lambda x: (impacts.Value + (impacts.Value * 0.006)) if x == 'SELL' else (impacts.Value - (impacts.Value * 0.006))))
错误:
TypeError: ufunc 'multiply' 不包含带有签名匹配类型的循环 dtype('S32') dtype('S32') dtype('S32' )
我的理解是错误是由数字的内容引起的,因此这就是我尝试将其转换为浮点数的原因。
预期输出
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
我们非常欢迎任何帮助。谢谢!
希望对您有所帮助:-)(仅修改了您自己的代码,您的示例代码将 return 出错)
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
得到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
我会用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])
分配回来后
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
(评论太长了)这是文的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
运算前用df.Order
判断符号
我有一个数据框,它取决于列 Order
的值我想获取列 Value
的值并进行一些计算。
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
代码行:
impacts['NewValue']=float(impacts.Order.apply(lambda x: (impacts.Value + (impacts.Value * 0.006)) if x == 'SELL' else (impacts.Value - (impacts.Value * 0.006))))
错误:
TypeError: ufunc 'multiply' 不包含带有签名匹配类型的循环 dtype('S32') dtype('S32') dtype('S32' )
我的理解是错误是由数字的内容引起的,因此这就是我尝试将其转换为浮点数的原因。
预期输出
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
我们非常欢迎任何帮助。谢谢!
希望对您有所帮助:-)(仅修改了您自己的代码,您的示例代码将 return 出错)
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
得到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
我会用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])
分配回来后
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
(评论太长了)这是文的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
运算前用df.Order
判断符号