简体   繁体   English

如何应用具有多个 dataframe 列的 function 作为 arguments?

[英]How to apply a function with several dataframe columns as arguments?

I'm trying to compute a new column in a pandas dataframe, based upon others columns, and a function I created.我正在尝试根据其他列和我创建的 function 在 pandas dataframe 中计算一个新列。 Instead of using a for loop, I prefer to apply the function with entires dataframe columns.而不是使用for循环,我更喜欢将 function 与整个 dataframe 列一起应用。

My code is like this:我的代码是这样的:

    df['po'] = vect.func1(df['gra'],
                           Se, 
                           df['p_a'], 
                           df['t'], 
                           Tc)

where df['gra'], df['p_a'], and df['t'] are my dataframe columns (parameters), and Se and Tc are others (real) parameters.其中 df['gra']、df['p_a'] 和 df['t'] 是我的 dataframe 列(参数),Se 和 Tc 是其他(实际)参数。 df['po'] is my new column. df['po'] 是我的新专栏。

func1 is a function described in my vect package. func1 是我的 vect package 中描述的 function。 This function is:这个 function 是:

def func1(g, surf_e, Pa, t, Tco):

    if (t <= Tco):
        pos = (g-(Pa*surf_e*g))
    else: 
        pos = 0.0
    return(pos)

When implemented this way, I obtain an error message, which concern the line: if (t <= Tco):当以这种方式实现时,我收到一条错误消息,涉及以下行: if (t <= Tco):

The error is: ValueError: The truth value of a Series is ambiguous.错误是: ValueError: Series 的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all() .使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

I read the pandas documentation, but didn't find the solution.我阅读了 pandas 文档,但没有找到解决方案。 Can anybody explain me what is the problem?谁能解释我有什么问题?

I tried to use apply :我尝试使用apply

for example:例如:

df['po'] = df['gra'].apply(vect.func1)

but I don't know how to use apply with multiples columns as parameters.但我不知道如何将 apply 与多个列一起用作参数。

Thank you by advance.提前谢谢你。

Use np.where with the required condition, value when the condition is True and the default value.np.where与所需条件、条件为 True 时的值和默认值一起使用。

df['po'] = np.where(
    df['t'] <= Tc,                               # Condition
    df['gra'] - (df['P_a'] * Se * df['gra']),    # Value if True
    0                                            # Value if False
)

EDIT:编辑:

Don't forget to import numpy as np不要忘记import numpy as np

Also, you get an error because you are comparing a series to a series and hence obtain a series of boolean values and not an atomic boolean value which if condition needs.此外,您会收到一个错误,因为您将一个系列与一个系列进行比较,因此获得一系列 boolean 值,而不是如果条件需要的原子 boolean 值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM