python - 根据 DataFrame A 中行中的值从 DataFrame B 中选择行

标签 python pandas

我有两个数据框。数据框 A是:

[distance]      [measure]
17442.77000     32.792658
17442.95100     32.792658
17517.49200     37.648482
17518.29600     37.648482
17565.77600     38.287118
17565.88800     38.287118
17596.93700     41.203340
17597.29700     41.203340
17602.16400     41.477979
17602.83900     41.612774
17618.16400     42.479890
17618.71100     42.681591

和数据框B这是:

[mileage]      [Driver]
17442.8         name1
17517.5         name2
17565.8         name3
17597.2         name4
17602.5         name5
17618.4         name6

对于每个 [mileage]数据框中的行 B , 我想从 [distance] 中找到两行在数据框中 A其中A.loc[(A['distance']>= milage_value) & A['distance']<= mileage_value]所以我可以有这样的东西:

17442.77000     32.792658
17442.8         name1
17442.95100     32.792658
17517.49200     37.648482
17517.5         name2
17518.29600     37.648482
.               .
.               .

所以我可以在大小为 3 的滚动窗口中应用以下函数:

def f(x):
    return df.iloc[0,1]+(df.iloc[2,1]-df.iloc[0,1])*((df.iloc[1,0]-df.iloc[0,0])/(df.iloc[2,0]-df.iloc[0,0]))
a = df.rolling(window=3, min_periods=1).apply(f)[::3].reset_index(drop=True)

到目前为止,我一直在连接两个 Df 并对值进行排序以生成上面的三元组,但是当来自 df B 的两个值时出现问题在 A[distance] 的距离范围内.非常感谢任何提示/建议!

最佳答案

我认为您可以使用 direction 参数和 drop_duplicates 将以下内容与 merge_asof 一起使用:

df_before = pd.merge_asof(df_a, df_b, 
                 left_on='distance', 
                 right_on='mileage', 
                 direction='backward')\
              .drop_duplicates(['mileage','Driver'], keep='first')[['distance','measure']]

df_after = pd.merge_asof(df_a, df_b, 
                         left_on='distance', 
                         right_on='mileage', direction='forward')\
             .drop_duplicates(['mileage', 'Driver'], keep='last')[['distance','measure']]

df_middle = df_b.rename(columns={'Driver':'measure','mileage':'distance'})

pd.concat([df_before, df_middle, df_after]).sort_values('distance').drop_duplicates()

输出:

     distance  measure
0   17442.770  32.7927
0   17442.800    name1
1   17442.951  32.7927
2   17517.492  37.6485
1   17517.500    name2
3   17518.296  37.6485
4   17565.776  38.2871
2   17565.800    name3
5   17565.888  38.2871
6   17596.937  41.2033
3   17597.200    name4
7   17597.297  41.2033
8   17602.164   41.478
4   17602.500    name5
9   17602.839  41.6128
10  17618.164  42.4799
5   17618.400    name6
11  17618.711  42.6816

关于python - 根据 DataFrame A 中行中的值从 DataFrame B 中选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50682490/

相关文章:

python - 为什么找不到 Python datetime time delta?

Python - 查找相同字符的序列

python - pd.cut 类别为 plt.xticklabels

python - 如何提高代码性能(使用 Google Translate API)

python - 如何在 QMainWindow MVC PyQt 的 closeEvent 中调用非 QObject 的方法

python - Pandas - 将多个文本文件中的信息合并到单个数据帧中

python - Pandas:删除重复记录,同时保留数据框中的旧值以供引用

python - 使用正则表达式重命名 Pandas 数据框中的列

excel - 使用 StyleFrame 的 to_excel 方法将多个 python 数据帧复制到 Excel 中

python - 如何检查连续相同的值和值的计数同时出现 pandas