如何在熊猫数据框中将系列传递给布尔函数?

我正在尝试使用用户定义的函数在Pandas数据框中创建一个新列,该列是根据其他列计算得出的。我的函数从Python库(psychrolib)调用其他函数来执行该操作。我收到以下错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty,a.bool(),a.item(),a.any() or a.all().

我知道您需要使用&,|和!在Pandas中工作时(而不是and和or,而不是)作为逻辑运算符,但是由于我使用的是库中的函数,因此这不是一个选择。我确定在库中运行了代码,以确保找到以下摘录:

    if (TDryBulb < -148 or TDryBulb > 392):
        raise ValueError("Dry bulb temperature must be in range [-148,392]°F")

是否有另一种方法可以将列传递给Pandas中的用户定义函数,还是应该使用其他数据类型?下面是一些示例代码:

# Import libraries
import numpy as np
import psychrolib as psych
psych.SetUnitSystem(psych.IP) # Set unit system in psychrometric library to IP
import pandas as pd

# create example data

df = pd.DataFrame({'T_dry_bulb':[80,82,84,86,88,90]})

def processvaporpressure(t_db):
    # Calculate the vapor pressure from the a psychrometric library function
    # and then do something special to it.
    return psych.GetSatVapPres(t_db) + 0.5

df['vapor_pressure'] = df.apply(processvaporpressure,args=(df['T_dry_bulb']))
chenruibo 回答:如何在熊猫数据框中将系列传递给布尔函数?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3148542.html

大家都在问