【问题标题】:Error TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]错误类型错误:无法将 dtyped [float64] 数组与 [bool] 类型的标量进行比较
【发布时间】:2020-03-02 21:52:55
【问题描述】:

我正在尝试计算在一个数据帧中完成两个条件的值以将其包含在另一个数据帧中,但出现错误

for index, row in data_orders_2_sample.iterrows():

    row['potential_couriers'] = ((data_courier_cash_balance_2_sample[data_courier_cash_balance_2_sample.acum_amount_eu >= 
                                                                                    row['purchase_price']]) 
                                 & (data_courier_cash_balance_2_sample.created_day == row['assignment_time'])
                                ).count()['courier_id']
    data_orders_2_sample.at[index,'potential_couriers'] = row['potential_couriers']

data_orders_2_sample.head()

错误是

TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

问题是,如果我评论这两个条件之一,代码会正常运行,所以错误在 & 但我不知道如何解决它。请帮忙?

谢谢大家:)

【问题讨论】:

  • 你能举个输入数据的例子吗?

标签: python


【解决方案1】:

我认为你想做这样的事情:

df =data_orders_2_sample
df['potential_couriers'] = df[
    (df.acum_amount_eu >= df.purchase_price) & (df.created_day == df.assignment_time)
].count()['courier_id']

【讨论】:

    【解决方案2】:

    感谢您的回答。我找到了另一种使用 numpy 的解决方法

    import numpy as np
    
    for index, row in data_orders_2.iterrows():
    
        row['potential_couriers'] = np.sum(((data_courier_cash_balance_2.acum_amount_eu) >= 
        row.purchase_price) & 
        (data_courier_cash_balance_2.created_day == 
        row.assignment_time))
        data_orders_2.at[index,'potential_couriers'] = row['potential_couriers']
    
    data_orders_2
    

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 2013-12-18
      • 1970-01-01
      • 2021-12-01
      • 2021-05-24
      • 2020-06-24
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多