简体   繁体   English

从另一个数据框中查找最接近的值的索引

[英]Finding index of a closest value from another dataframe

I have two dataframes measuring two properties from an instrument, where the depths are offset for a certain dz. 我有两个数据框,用于测量仪器的两个属性,其中深度对于特定dz而言是偏移的。 Note that the example below is extremely simplified. 请注意,以下示例已大大简化。

df1 = pd.DataFrame({'depth_1': [0.936250, 0.959990, 0.978864, 0.991288, 1.023876, 1.045801, 1.062768, 1.077090, 1.101248, 1.129754, 1.147458, 1.160193, 1.191206, 1.218595, 1.256964] })

df2 = pd.DataFrame({'depth_2': [0.620250, 0.643990, 0.662864, 0.675288, 0.707876, 0.729801, 0.746768, 0.761090, 0.785248, 0.813754, 0.831458, 0.844193, 0.875206, 0.902595, 0.940964 ] })

How do I get the index of df2.depth_2 that gets closest the first element of df1.depth_1 ? 如何获取最接近df2.depth_2 的第一个元素的df1.depth_1

Using reindex with method nearest reindexnearest方法一起使用

df2.reset_index().set_index('depth_2').reindex(df1.depth_1,method = 'nearest')['index'].unique()
Out[265]: array([14], dtype=int64)

You can use pandas merge_asof function (you will need to order your data first if it isn't in real life) 您可以使用pandas merge_asof函数(如果不在现实生活中,则需要先订购数据)

df1 = df1.sort_values(by='depth_1')
df2 = df2.sort_values(by='depth_2')
pd.merge_asof(df1, df2.reset_index(), left_on="depth_1", right_on="depth_2", direction="nearest")

if you just wanted that for the first value in df1 you could do the join on the top row: 如果您只是想要df1中的第一个值,则可以在第一行进行连接:

df2 = df2.sort_values(by='depth_2')
pd.merge_asof(df1.head(1), df2.reset_index(), left_on="depth_1", right_on="depth_2", direction="nearest")

Get the absolute difference between all elements of df2 and first element of df1 and then get it's index: 获取df2所有元素与df1第一个元素之间的绝对差,然后获取其索引:

import pandas as pd
import numpy as np

def get_closest(df1, df2, idx):
   abs_diff = np.array([abs(df1['depth_1'][idx]-item) for item in df2['depth_2']])
   return abs_diff.argmin()

df1 = pd.DataFrame({'depth_1': [0.936250, 0.959990, 0.978864, 0.991288, 1.023876, 1.045801, 1.062768, 1.077090, 1.101248, 1.129754, 1.147458, 1.160193, 1.191206, 1.218595, 1.256964] })

df2 = pd.DataFrame({'depth_2': [0.620250, 0.643990, 0.662864, 0.675288, 0.707876, 0.729801, 0.746768, 0.761090, 0.785248, 0.813754, 0.831458, 0.844193, 0.875206, 0.902595, 0.940964 ] })

get_closest(df1,df2,0)

Output: 输出:

14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM