【问题标题】:Returning a unique row if condition returns multiple rows如果条件返回多行,则返回唯一行
【发布时间】:2020-04-02 07:02:01
【问题描述】:

我正在为车辆路线问题编写算法。当从某个种子城市到其他城市的行驶时间最短时,应该将其添加到我的路线中。为了检查这一点,我使用

new_point_row_df = emte_df[emte_df["seed_time"] == emte_df.seed_time.min()]

这给了我新城市的整排。然后我用

lat = new_point_row_df["Lat"].squeeze()
long = new_point_row_df["Long"].squeeze()

这给了我那个城市的坐标。然后我将这些坐标与我的“家”坐标(配送中心所在的坐标)一起传递给函数“time_to_home” 毫无疑问,它计算了 time_to_home。

time_to_home((lat,long), veghel_coordinates)

在大多数情况下,这工作得很好。然而,在emte_df.seed_time.min()返回多行的极少数情况下(所以从我的种子点出发,两个城市的行驶时间完全相同),纬度和经度是熊猫系列而不是 numpy 浮动。当我将系列传递给我的 time_to_home 函数时,它会崩溃并抛出

TypeError: cannot convert the series to <class 'float'>这很有意义。

我的问题是:如果emte_df.seed_time.min()returns 多行,我怎样才能使我只选择 one 来传递到我的纬度/经度坐标和随后的 time_to_home 函数?选择哪一行并不重要,只要它只有一个,我的 time_to_home 函数就不会崩溃。

【问题讨论】:

    标签: python pandas dataframe unique


    【解决方案1】:

    简单地做:

    ew_point_row_df = emte_df[emte_df["seed_time"] == emte_df.seed_time.min()].iloc[0]
    

    这样每次只会选择第一行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-25
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      相关资源
      最近更新 更多