简体   繁体   English

如何迭代一个 dataframe 的每一行并与 Python 中的另一个 dataframe 中的行进行比较?

[英]how to iterate each row of one dataframe and compare with rows in another dataframe in Python?

I have two dataframes:我有两个数据框:

DF1: DF1:

ID     v1           v2         v3
289  1455.0        2.0        0.62239  
289  1460.0        0.0        0.46037  
289  1465.0        4.0        0.41280 
290  1470.0        0.0        0.39540 
290  1475.0        2.0        0.61809 
290  1475.0        2.0        0.61809

DF2: DF2:

ID     v1           v2         v3
289  1423.0        2.0        0.62239  
289  142Q.0        0.0        0.46037  
289  14FW.0        4.0        0.41280  
290  14Q3.0        0.0        0.39540  
290  1453.0        2.0        0.61809 
290  1454.0        2.0        0.61809

I want to iterate each row in DF1 with every row in DF2 and see if it is in DF2, something like:我想用 DF2 中的每一行迭代 DF1 中的每一行,看看它是否在 DF2 中,例如:

for row in results_01.iterrows():
    diff = []
    if row not in results_02:
        add different one to 'diff'
        print(diff)

I know the logic but not sure how to do this, new to Python, can anyone help me?我知道逻辑但不知道该怎么做,Python 的新手,谁能帮帮我? Many thanks.非常感谢。

You can do it easily with 'inner' merge.您可以使用“内部”合并轻松完成。

intersect = pd.merge(df1, df2, how='inner')

Edit:编辑:

It turns out that the rows that are in df1 and not in df2 are wanted and not the intersection.事实证明,需要 df1 而不是 df2 的行而不是交集。 In this case one should use the isin pandas method.在这种情况下,应该使用isin pandas 方法。 Here is SO link that deals with it.这是处理它的SO链接

One way to do it (maybe not the most efficient) would be to append the dataframes together and then drop duplicates, like so:一种方法(可能不是最有效的)是将 append 数据帧放在一起,然后删除重复项,如下所示:

full_df = df1.append(df2)
full_df = full_df.drop_duplicates(keep=False)

The code block you have looks pretty close to what you do in python.您拥有的代码块看起来与您在 python 中所做的非常接近。 Take a row from one dataframe and iterate through the other dataframe looking for matches.从一个 dataframe 中取一行,然后遍历另一个 dataframe 寻找匹配项。

for index, row in results_01.iterrows():
    diff = []
    compare_item = row['col_name'] 
    for index, row in results_02.iterrows():
       if compare_item == row['compare_col_name']:
           diff.append(compare_item, row['col_name']
    return diff 

Here I am taking a specific column value from a row from one dataframe and comparing it to another value from the other dataframe在这里,我从一个 dataframe 的一行中获取一个特定的列值,并将其与另一个 dataframe 的另一个值进行比较

暂无
暂无

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

相关问题 将一个数据帧中的每一行与 Python 中另一个数据帧中的每一行进行比较 - Compare each row in one dataframe to each row in another dataframe in Python 如何将一个数据框的每一行与另一数据框的所有行进行比较,并计算距离度量? - How to compare each row from one dataframe against all the rows from other dataframe and calculate distance measure? 比较一个数据框中的两列的行(如果存在) 蟒蛇 - Compare the rows of two columns in one dataframe if they exist another dataframe | python 将数据帧的一行与其他数据帧的行进行比较? - Compare one row of a dataframe with rows of other dataframe? Python:将一个DataFrame中的行除以另一个DataFrame中的所有行 - Python: Divide row in one DataFrame by all rows in another DataFrame 将每一行与数据框中的其他行进行比较 - Compare each row to the other rows in a Dataframe 如何迭代 dataframe 行并比较 if 条件中的值 - How to iterate dataframe row and compare the values in if condition Python - 如何遍历 dataframe 到 append 每一行 - Python - How to iterate through dataframe to append each row Python-如何遍历数据框并将一个单元格中的值替换为同一行中另一个单元格的值 - Python - How to iterate through a dataframe and replace a value in one cell with a value from another in the same row 在Python中搜索另一个数据框中的一个数据框的行 - Searching rows of one dataframe in another dataframe in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM