【问题标题】:Custom distance function between every row of two dataframes两个数据帧的每一行之间的自定义距离函数
【发布时间】:2021-09-15 01:49:12
【问题描述】:

我有两个数据帧,我想计算一个数据帧中每一行与另一个数据帧中每一行之间的“距离”,使用自定义距离度量(例如第一列的欧几里德,第二列的出租车等)。有没有办法使用广播快速做到这一点?

【问题讨论】:

    标签: pandas


    【解决方案1】:

    您可以简单地创建一个自定义函数并在应用函数中使用它:

    例如:

    def custom_func(a, b):
        result = a + b for example: 
        return result
    
    df = df.apply(custom_func)
    

    如果您想要需要在某些数据集中测试的答案,请添加数据。但想法是您可以创建任何自定义函数并传入您的 pandas 应用函数。 更多关于 apply 函数和例子是:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html

    您也可能想检查 pandas 中的 applymap 函数。 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.applymap.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-11
      • 2021-01-23
      • 2019-12-25
      • 2015-02-12
      • 2018-10-09
      • 2020-02-06
      • 2014-04-09
      相关资源
      最近更新 更多