数据框的头
strike,timestamp,optionType,IV,OptPrice,volume,spot,riskFree,divYield,mdate,ttm,volatility
11500.0,2019-04-03 09:21:23.035218,call,14.4,372.05,103.0,11713.2,0.0625,0.0112,2019-04-25 00:00:00.000000,0.05916,0.201913
11500.0,2019-04-03 09:21:23.035218,put,16.49,68.1,1173.0,11713.2,0.0625,0.0112,2019-04-25 00:00:00.000000,0.05916,0.144977
11500.0,2019-04-03 09:21:28.035640,call,14.4,372.05,103.0,11713.2,0.0625,0.0112,2019-04-25 00:00:00.000000,0.05916,0.201913
11500.0,2019-04-03 09:21:28.035640,put,16.49,68.1,1173.0,11713.2,0.0625,0.0112,2019-04-25 00:00:00.000000,0.05916,0.144977
11500.0,2019-04-03 09:21:33.036651,call,14.4,372.05,103.0,11713.2,0.0625,0.0112,2019-04-25 00:00:00.000000,0.05916,0.201914
可以通过复制粘贴代码的方式读取到数据框中
import pandas as pd
df = pd.read_clipboard(sep=',')
def greeks(optionType, spot, strike, riskFree, ttm, vol, divYield=0):
<do something>
return [speed, zomma, color, ultima]
af = df.apply(lambda row: greeks(row["optionType"], row["spot"], row["strike"], row["riskFree"], row["ttm"], row["volatility"], row["divYield"]), axis=1)
给我这个错误:
Shape of passed values is (5, 4), indices imply (5, 12)
我尝试遵循SO answer的这个this和第二条评论,但不起作用。
df['speed'], df['zomma'], df['color'], df['ultima'] = zip(*df.apply(lambda row: greeks(row["optionType"], row["spot"], row["strike"], row["riskFree"], row["ttm"], row["volatility"], row["divYield"]), axis=1))
同样,我收到错误消息。
如果我传递的df
的子集的列数与计算希腊文的函数greeks
的参数数完全相同,则该代码有效。
就像下面的代码一样:
xf = df.iloc[:,[0,2,6,7,8,10,11]]
ef = xf.apply(lambda row: greeks(row["optionType"], row["spot"], row["strike"], row["riskFree"], row["ttm"], row["volatility"], row["divYield"]), axis=1)
ef = pd.DataFrame(item for item in ef)
xf = pd.concat([sf, cf], axis='columns')
但这是一个麻烦。我们可以这样做,以便将该功能应用于原始数据帧而无需子集,然后重新附加到原始数据帧