简体   繁体   English

如何比较两个数据框中的坐标?

[英]How to compare coordinates in two dataframes?

I have two dataframes我有两个数据框

df1 df1

x1 x1 y1 y1 x2 x2 y2 y2 label label
0 0 0 0 1240 1240 1755 1755 label1标签 1
0 0 0 0 1240 1240 2 2个 label2标签2

df2 DF2

x1 x1 y1 y1 x2 x2 y2 y2 text文本
992.0 992.0 943.0 943.0 1166.0 1166.0 974.0 974.0 tex1 tex1
1110.0 1110.0 864.0 864.0 1166.0 1166.0 890.0 890.0 text2文字2

Based on a condition like the following:基于如下条件:

if df1['x1'] >= df2['x1'] or df1['y1'] >= df2['y1']:
   # I want to add a new column 'text' in df1 with the text from df2.
   df1['text'] = df2['text']

What's more, it is possible in df2 to have more than one row that makes the above-mentioned condition True , so I will need to add another if statement for df2 to get the best match.更重要的是,在df2中可能有不止一行使上述条件为True ,因此我需要为 df2 添加另一个if语句以获得最佳匹配。

My problem here is not the conditions but how am I supposed to approach the interaction between both data frames.我的问题不是条件,而是我应该如何处理两个数据帧之间的交互。 Any help, or advice would be appreciated.任何帮助或建议将不胜感激。

If you want to iterate from df1 through every row of df2 and return a match you can do it with the .apply() function in df1 and use the df2 as lookup table.如果您想从 df1 遍历 df2 的每一行并返回一个匹配项,您可以使用df1中的.apply() function 并使用df2作为查找表。

NOTE: In the above example I return the first match (by using the .iloc[0] ) not all the matches.注意:在上面的示例中,我返回第一个匹配项(通过使用.iloc[0] )而不是所有匹配项。

Create two dummy dataframes创建两个虚拟数据框

import pandas as pd

df1 = pd.DataFrame({'x1': [1, 2, 3], 'y1': [1, 5, 6]})
df2 = pd.DataFrame({'x1': [11, 1, 13], 'y1': [3, 52, 26], 'text': ['text1', 'text2', 'text3']})

Create a lookup function创建查找 function

def apply_condition(row, df):
    condition  = ((row['x1'] >= df['x1']) | (row['y1'] >= df['y1']))
    return df[condition]['text'].iloc[0] # ATTENTION: Only the first match return

Create new column and print results创建新列并打印结果

df1['text'] = df1.apply(lambda row: apply_condition(row, df2), axis=1)
df1.head()

Result:结果:

在此处输入图像描述

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

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