0

Head of the dataframe

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

Can be read into a dataframe by copy pasting with the code

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)

Gives me this error:

Shape of passed values is (5, 4), indices imply (5, 12)

I tried to follow this SO answer and 2nd comment on this, but it doesn't work.

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))

Again I get the error message as before.

If I pass the subset of df having exactly same number of columns as the number of arguments of the function greeks, which calculates the greeks, the code works.

Like the code below:

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')

But this is a hassle . Can we make so that the function can be applied to the original dataframe without to subsetting and then reattaching back to the original dataframe

Frash
  • 724
  • 1
  • 10
  • 19

0 Answers0