【问题标题】:Function Issue - Truth Value is Ambiguous函数问题 - 真值不明确
【发布时间】:2019-02-05 03:42:42
【问题描述】:

我正在构建一个函数来计算起始纬度和经度与停止纬度和经度之间的距离。我创建了一个变量,它工作正常,但是当我创建一个函数时,我得到了消息;

'Series 的真值是模棱两可的。使用 a.empty、a.bool()、 a.item(), a.any() 或 a.all().', '发生在索引 0'

下面是我的功能。任何帮助将不胜感激! (注意,正在使用的列没有空值,并且都是 float64。Orders 是正在使用的数据框。)。

def distance_calc (row):
    start = (orders['start_lat'], orders['start_lon'])
    stop = (orders['stop_lat'], orders['stop_lon'])

    return great_circle(start,stop).meters

orders['distance'] = orders.apply(lambda orders: distance_calc (orders), axis=1)

【问题讨论】:

    标签: python-3.x pandas function


    【解决方案1】:

    需要选择Series 称为row 而不是orders

    def distance_calc (row):
        start = (row['start_lat'], row['start_lon'])
        stop = (row['stop_lat'], row['stop_lon'])
    
        return great_circle(start,stop).meters
    
    #lambda is not necessary
    orders['distance'] = orders.apply(distance_calc, axis=1)
    

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 2018-07-06
      • 2017-03-27
      • 1970-01-01
      • 2022-10-05
      • 2021-09-10
      • 2020-08-03
      • 1970-01-01
      • 2023-01-08
      相关资源
      最近更新 更多