首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过dataframe运行函数并接收相同的值

通过dataframe运行函数并接收相同的值
EN

Stack Overflow用户
提问于 2021-03-16 16:24:22
回答 1查看 31关注 0票数 0

我正在尝试运行此函数:

代码语言:javascript
复制
def delta(d_1, contract_type):
    if contract_type == 'c':
        return norm.cdf(d1)
    if contract_type == 'p':
        return -norm.cdf(-d_1)

通过数据帧执行以下操作:

代码语言:javascript
复制
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')

但得到与result相同的值:

代码语言:javascript
复制
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

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2021-03-17 17:59:59

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

如果你应用一个常量lambda函数,那么你会得到一个常量序列。例如:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66651438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档