如果条件返回多行,则返回唯一行

时间:2020-04-02 07:02:01

标签: python pandas dataframe unique

我正在写一个关于车辆路径问题的算法。当从一个种子城市到另一个城市的驾驶时间最少时,应将其添加到我的路线中。为此,我使用

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()

哪个给我那个城市的坐标。然后,将这些坐标与“ home”坐标(配送中心所在的坐标)一起传递给函数“ time_to_home” 可以计算出time_to_home,这并不奇怪。

time_to_home((lat,long), veghel_coordinates)

在大多数情况下,这非常正常。但是,在极少数情况下,emte_df.seed_time.min()返回多行(因此,从我的种子点开始,两个城市完全位于 相同的行驶时间),纬度和经度是 pandas系列而不是numpy浮点数。当我将系列传递给time_to_home函数时,它分解并抛出

TypeError: cannot convert the series to <class 'float'>十分合理。

我的问题是:如果emte_df.seed_time.min()返回多行,我该如何做,以便仅选择一个传递到纬度/经度坐标中,并随后传递给time_to_home函数?选择哪一行都没有关系,只要只有一行,这样我的time_to_home函数就不会崩溃。

1 个答案:

答案 0 :(得分:1)

只需:

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

这只会每次选择第一行。

相关问题