通过数据帧运行函数并接收相同的值

问题描述

我正在尝试运行此功能

def delta(d_1,contract_type):
    if contract_type == 'c':
        return norm.cdf(d1)
    if contract_type == 'p':
        return -norm.cdf(-d_1)

通过执行此操作通过数据框:

S = test.strike
K = test.stock_price_close
t = test.TtM/365
r = test.Rf
sigma = test.VSMI
test.apply(lambda x: delta(d1,'p'),axis=1,raw=False,result_type='expand')

但得到与结果相同的值:

2001-11-01   -0.752478
2001-11-02   -0.752478
2001-11-05   -0.752478
2001-11-06   -0.752478
2001-11-08   -0.752478
                ...   
2006-10-26   -0.752478
2006-10-27   -0.752478
2006-10-30   -0.752478
2006-10-31   -0.752478
2006-11-01   -0.752478
Length: 960,dtype: float64

有什么想法吗?

解决方法

如果您使用变量 x,该函数只会返回一个变量答案。

如果您应用常量 lambda 函数,那么您将收到一个常量系列。例如

import pandas as pd
df = pd.DataFrame([1,2,3,4],columns=['col1'])
df['col1'].apply(lambda x: 5) # constant lambda function - x is not used

# constant series returned

0    5
1    5
2    5
3    5
Name: col1,dtype: int64