简体   繁体   English

Python Pandas匹配距离另一个Dataframe最近的索引

[英]Python Pandas matching closest index from another Dataframe

df.index = 10,100,1000

df2.index = 1,2,11,50,101,500,1001
Just sample

I need to match closest index from df2 compare with df by these conditions 我需要匹配来自df2的最接近的索引与这些条件下的df匹配

  1. df2.index have to > df.index df2.index必须> df.index
  2. only one closest value 只有一个最接近的值

for example output 例如输出

df     |   df2
10     |   11
100    |   101
1000   |   1001

Now I can do it with for-loop and it's extremely slow 现在我可以使用for循环来完成它并且它非常慢

And I used new_df2 to keep index instead of df2 我使用new_df来保持索引而不是df2

new_df2 = pd.DataFrame(columns = ["value"])
for col in df.index:
    for col2 in df2.index:
        if(col2 > col):
            new_df2.loc[col2] = df2.loc[col2]
            break
        else:
            df2 = df2[1:] #delete first row for index speed

How to avoid for-loop in this case Thank. 在这种情况下如何避免for循环谢谢。

Not sure how robust this is, but you can sort df2 so it's index is decreasing, and use asof to find the most recent index label matching each key in df 's index: 不确定这是多么强大,但你可以对df2进行排序,使它的索引正在减少,并使用asof来查找匹配df索引中每个键的最新索引标签:

df2.sort_index(ascending=False, inplace=True)
df['closest_df2'] = df.index.map(lambda x: df2.index.asof(x))

df
Out[19]: 
      a  closest_df2
10    1           11
100   2          101
1000  3         1001

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

相关问题 熊猫:从另一个数据框中找到最接近的组 - Pandas: Find closest group from another dataframe 从另一个数据框中查找最接近的值的索引 - Finding index of a closest value from another dataframe 在 pandas 中为每一行从另一个 dataframe 检索最接近的值 - Retrieve closest value from another dataframe in pandas for each row 有没有一种方法可以遍历Pandas中的一列以从另一个数据框中找到匹配的索引值? - Is there a way to iterate over a column in Pandas to find matching index values from another dataframe? Python Pandas:在 dataframe 中过滤匹配来自另一个 dataframe 的一组多个项目/条件 - Python Pandas: Filter in dataframe matching a set of multiple items / conditions from another dataframe 按最接近的索引连接熊猫DataFrame值 - Join pandas DataFrame values by closest index Python Dataframe 找到具有公差的最接近匹配值 - Python Dataframe find closest matching value with a tolerance Python Pandas - 识别 Dataframe 列中第一个匹配值的索引 - Python Pandas - Identifying the index of first matching value in a Dataframe column 通过匹配变量将值从一个 pandas dataframe 添加到另一个 dataframe - Adding value from one pandas dataframe to another dataframe by matching a variable Python Pandas DataFrame:将列名称与行索引匹配” - Python Pandas DataFrame: Matching column names to row index'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM