【问题标题】:The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() using panda pythonSeries 的真值是模棱两可的。使用熊猫 python 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
【发布时间】:2019-05-04 09:59:58
【问题描述】:

这里我有要从 CSV 文件导入的数据。我在类中编写了一个方程,并求解方程数据将从 CSV 文件导入。 当我运行我的代码时,我得到了一个错误,比如“元组索引必须是整数或切片,而不是 str”,使用 panda python。 谁能帮我解决这个问题? 我在这里上传我的代码和 CSV 文件。

def time_convert(x):
    h,m,s = map(int,x.split(':'))
    return (h*60+m)

def ph_convert(time,we,h,a,w):
    while time <= 30:
        level = 1.1
        level = float(level)
        if w == 1:
            ph= ((((6*we)+(1*h))/level -(4*a)))/time
        else:
            ph= ((6+((1*we)+(3*h))/level -(6 *a)))/time  
        break
    while time <=60:
        level = 1.25
        level = float(level)
        if w == 1:
            ph= ((((6*we)+(1*h))/level -(4*a)))/time
        else:
            ph= ((6+((1*we)+(3*h))/level -(6 *a)))/time  
        break
    print(ph)

data = pd.read_csv('data1.csv')
data['time'] = data['time'].apply(time_convert)
we = data['we'].astype(float)
h = data['h'].astype(float)
a = data['a'].astype(float)
w = data['w'].astype(float)
time = data['time'].astype(float)
print(ph_convert(time,we,h,a,w))

我的 CSV 文件的子集:

we      h   a   w   time
48.1    150 53  1   6:15:00
48.1    150 53  1   9:00:00
48.1    150 53  1   9:25:00
48.1    150 53  1   9:30:00
48.1    150 53  1   11:00:00

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-922-9fe360350b03> in <module>()
----> 1 print(ph_convert(time,we,h,a,w))

<ipython-input-904-73455dabb050> in ph_convert(time, we, h, a, w)
      3    
----> 5         while time <= 30:
      6             level = 1.1
      7             level = float(level)

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1571         raise ValueError("The truth value of a {0} is ambiguous. "
   1572                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1573                          .format(self.__class__.__name__))
   1574 
   1575     __bool__ = __nonzero__

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

【问题讨论】:

  • time_convert 函数在做什么?
  • @MohitMotwani 它将我的时间转换为分钟。代码是:-def time_convert(x): h,m,s = map(int,x.split(':')) return (h*60+m)
  • time(和其他变量)实际上是一个系列。您的函数需要 time(int, float) 的单个值而不是系列。因此,您会收到此错误。
  • @MohitMotwani 你能给我一些解决这个错误的建议吗?我没有任何想法来解决这个问题。
  • 您想要一个新列,其中每一行都有自己的 ph_convert 值吗?

标签: python pandas


【解决方案1】:

您可以将while 更改为ifelif 并添加else 如果time &gt; 60

def ph_convert(time,we,h,a,w):
    if time <= 30:
        level = 1.1
        if w == 1:
            ph= ((((6*we)+(1*h))/level -(4*a)))/time
        else:
            ph= ((6+((1*we)+(3*h))/level -(6 *a)))/time  
    elif time <=60:
        level = 1.25
        if w == 1:
            ph= ((((6*we)+(1*h))/level -(4*a)))/time
        else:
            ph= ((6+((1*we)+(3*h))/level -(6 *a)))/time  
    else:
        ph = np.nan

    return ph

并将值分配给列:

data['time'] = data['time'].apply(time_convert).astype(float)
data['we'] = data['we'].astype(float)
data['h'] = data['h'].astype(float)
data['a'] = data['a'].astype(float)
data['w'] = data['w'].astype(float)

最后一次调用applyaxis=1 获取每行进程数:

print(data.apply(lambda x: ph_convert(x.time,x.we,x.h,x.a,x.w), axis=1))

【讨论】:

  • @awa - 使用样本数据返回 NaN,因为没有定义如果 time &gt; 60 会发生什么。
  • @awa - 如果time &gt; 60 需要返回什么?
  • @awa - 因为如果检查 print (time) 它返回时间 > 60
  • @awa - 不确定,但您需要从 def time_convert(x): 返回 - > return (h*60+m) ?不是return m?还是别的什么?
  • @jerzrael 我可以这样写 if time>60 then ph =2500 吗?
【解决方案2】:

你可以像这样改变你的功能:

def ph_convert(x):
    print(x.time)
    print(type(x.time))
    ph = 2500
    if x.time <= 30:
        level = float(1.1)
        if x.w == 1:
            ph= ((((6*x.we)+(1*x.h))/level -(4*x.a)))/x.time
        else:
            ph= ((6+((1*x.we)+(3*x.h))/level -(6 *x.a)))/x.time  
    elif x.time <=60:
        level = 1.25
        if x.w == 1:
            ph= ((((6*x.we)+(1*x.h))/level -(4*x.a)))/x.time
        else:
            ph= ((6+((1*x.we)+(3*x.h))/level -(6 *x.a)))/x.time
    return ph

然后使用apply:

data['ph'] = data.apply(ph_convert, axis =1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2021-08-12
    • 1970-01-01
    • 2018-03-28
    • 2021-09-09
    • 2021-11-20
    相关资源
    最近更新 更多