【问题标题】:apply function on dataframe involving two rows在涉及两行的数据帧上应用函数
【发布时间】:2014-10-03 01:34:56
【问题描述】:

我有一个包含 3 列和 00 行的 pandas DataFrame:

    time      longitude      latitude
    1         x1              y1
    2         x2              y2
    3         x3              y3
    ... 

我想应用一个函数来根据经度和纬度计算距离。基本上我需要一种方法来表达函数可以处理数据框中的两个相邻行 喜欢

    compute_distance(x1,y1,x2,y2)

我知道有一些方法可以沿轴 1 和 0 应用函数,但它们似乎只适用于单行或单列。如何表达涉及多行或多列的内容。

【问题讨论】:

  • 如果您只是想要差异,那么只需致电diff 进行计算

标签: python pandas


【解决方案1】:

Apply 无法做到这一点,但您可以执行以下简单操作:

def compute_distance(df):
    next_df = df.shift(-1)
    return distance_on_unit_sphere(df["lat"], df["long"],
                                   next_df["lat"], next_df["long"]):

从这里 http://www.johndcook.com/python_longitude_latitude.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2019-11-15
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多