简体   繁体   English

我如何使用 df 的多列作为 function 的输入?

[英]how do i use multiple columns of a df as input to a function?

ihave a dataframe full of numerical values.我有一个充满数值的 dataframe。 and i want to apply a function (delta) it takes 6 arguements.我想申请 function (delta) 它需要 6 个参数。

calls['delta'] = calls[['callput','underlyinglast','strike','yte','rfr','hvol90']].apply(delta,axis=1)

i keep getting the error我一直收到错误

TypeError: delta() missing 5 required positional arguments: 'S', 'K', 't', 'r', and 'sigma'

i also tried a variant with lambda, but i keep getting the error.我还尝试了 lambda 的变体,但我一直收到错误消息。 i think because its trying to apply the function to each individual value, instead of the row.我认为是因为它试图将 function 应用于每个单独的值,而不是行。

can i do this any other way?我可以用其他方式做到这一点吗? (without iterrows, its so slow, its a df with 500k rows) (没有 iterrows,它太慢了,它是一个有 500k 行的 df)

apply with axis=1 calls your function for each row, but with 1 parameter: the row as a Series object. So you either need to revise your function definition to take a single row instead of multiple parameters, or wrap your function call in a lambda function which extracts the values from each row and calls the function with them. apply with axis=1为每一行调用 function,但有 1 个参数:该行作为Series object。因此,您需要修改 function 定义以采用单行而不是多个参数,或者将 function 调用包装在一个lambda function 从每一行中提取值并调用 function。

  1. Revising your function to take a single row修改你的 function 取单行
    Instead of this:而不是这个:

     def delta(S, K, t, r, sigma): #...

    do this:做这个:

     def delta(row): S, K, t, r, sigma = row.tolist() #...
  2. Wrapping your function call in a lambda function将您的 function 电话包装成 lambda function
    Instead of this:而不是这个:

     calls['delta'] = calls[['callput','underlyinglast','strike','yte','rfr','hvol90']].apply(delta,axis=1)

    do this:做这个:

     calls['delta'] = calls[['callput','underlyinglast','strike','yte','rfr','hvol90']].apply(lambda row: delta(*row), axis=1)

    (the trick there is to use lambda row: delta(*row) instead of just delta ; *row basically "spreads" the items in row across the separate arguments of delta ) (这里的技巧是使用lambda row: delta(*row)而不仅仅是delta*row基本上将row中的项目“传播”到delta的单独 arguments 中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在应用 function 中使用 df.astype() - How do I use df.astype() inside apply function pandas 0.20:带有多级索引列的 df - 如何过滤多列的条件? - pandas 0.20: df with columns of multi-level indexes - How do I filter with condition on multiple columns? 如何获得2组df列? - How do I get 2 sets of columns of a df? 当我将 function 应用于 DF 以创建多个新列时,我得到不同的结果,具体取决于我提交的 DF 中的行数 - When I apply a function to a DF to create multiple new columns, I get different results depending how many rows are in the DF I submit 如何将输入输入 python df? - How do I put input into a python df? 如何在第一个df的列中使用包含function来过滤索引的列 - How to use the contain function in the columns of the first df to filter with the columns of the index 如何使用另一列中的一个键将pandas df与多列合并? - How do I merge pandas df with multiple columns using one key from another column? 如何将 df 的多列拆分为多行? - How can I split multiple columns of a df into multiple rows? 如何将 n 列的 DF 重塑为具有 3 列的统一 DF,其中 n 始终是 3 的倍数? - How could I reshape a DF of n columns into a unified DF with 3 columns where's n is always a multiple of 3? 如何对具有多列的df重新采样 - How to resample a df with multiple columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM