Home > Blockchain >  Trying to get value in column of dataframe using another columns data using function?
Trying to get value in column of dataframe using another columns data using function?

Time:03-22

I currently have a data frame "Finaldf" consisting of a column (underlying_price, strike, rate, days_to_exp, price,IV). that looks like this-

import pandas as pd
import mibian
stocksdf = {'underlying_price': [82600,38900,28775,28900,28275],
            'strike': [30400,19050,34000,36500,34500],
            'rate': [0,0,0,0,0],
            'days_to_exp': [3,3,3,3,3],
            'price': [12,3,4,8,3.5],
            'Opt_type': ['CE', 'PE', 'PE', 'PE', 'PE']}
final=pd.DataFrame(stocksdf)
final['IV']=""
print(final)

output-

   underlying_price  strike  rate    days_to_exp  price Opt_type  IV
0             82600   30400   3.81            3   12.0       CE       
1             38900   19050   3.81            3    3.0       PE       
2             28775   34000   3.81            3    4.0       PE       
3             28900   36500   3.81            3    8.0       PE       
4             28275   34500   3.81            3    3.5       PE       

and I have a function to calculate the "ImpVol" column of "final" data frame that looks like this:

def impliedVol_Call(underlying_price, strike, rate, days_to_exp, price):
    c = mibian.BS([underlying_price, strike, rate,
                  days_to_exp], callPrice=price)
    Call_IV = c.impliedVolatility
    return Call_IV



def impliedVol_Put(underlying_price, strike, rate, days_to_exp, price):
    p = mibian.BS([underlying_price, strike, rate,
                  days_to_exp], putPrice=price)
    Put_IV = p.impliedVolatility
    return Put_IV

So, I tried to calculate "IV" column like this-

for i in range(len(final)):
if pd.isna(final["Opt_type"].iloc[i]=='CE'):
    final['IV'].iloc[i]=impliedVol_Call(final['Underlying_price'][i],final['strike'][i],final['rate'][i],final['time_toEx'][i],final['Premium_price'][i])
else:
    final['IV'].iloc[i]=impliedVol_Put(final['Underlying_price'][i],final['strike'][i],final['rate'][i],final['time_toEx'][i],final['Premium_price'][i])

Please help me to get the column of ImVol(IV).

CodePudding user response:

Well, what you are doing is in iterative manner. You can explore the lambdas function and apply methods over dataframe.

Below is the sample code, which you can alter as per the need. Since, i don't have your function methodology of impliedVol_Put , i can only suggest the method of how you can alter this.

final['ImpVol'] = final.apply(lambda x: impliedVol_Call(final['Underlying_price'][i],final['strike'][i],final['rate'][i],final['time_toEx'][i],final['Premium_price'][i])
                    if pd.isna(final["Opt_type"].iloc[i]=='CE') else impliedVol_Put(final['Underlying_price'][i],final['strike'][i],final['rate'][i],final['time_toEx'][i],final['Premium_price'][i]),
                    axis=1)

CodePudding user response:

Maybe possible to call function impliedVol_Call inside lambda with columns as arguments.

finaldf['ImpVol']=finaldf.apply(lambda x:impliedVol_Call(x[0],x[1],x[2],x[3],x[4]))
  • Related