[英]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.