[英]Locate rows in one dataframe based on criteria from another dataframe
I have created two Dataframes from a main dataframe, eg df1 and df2. 我已经从主数据帧(例如df1和df2)创建了两个数据帧。
Each dataframe has the same no of columns, but no of rows in df2 will be less than in df1. 每个数据框具有相同的列数,但df2中的行数少于df1中的行数。 The dataframe will have columns waferlot,x,y,w.
数据框将具有一列waferlot,x,y,w。
How can search for waferlot,x,y,w from df2 in df1. 如何在df1中从df2搜索晶圆,x,y,w。
While there can be multiple ways to achieve this, one of which has been mentioned in the comments, but I usually do it using isin : 尽管有多种方法可以实现此目的,但注释中已经提到了其中一种,但是我通常使用isin来实现 :
Given two dataframes: 给定两个数据框:
import pandas as pd
df1 = pd.DataFrame()
df1['C1'] = ['a', 'b', 'c', 'd', 'e', 'f']
df1['C2'] = ['b', 'c', 'x', 'w', 'h', 'j']
df2 = pd.DataFrame()
df2 ['C1'] = ['x', 'a', 'c', 'f']
df2 ['C2'] = ['w', 'h', 'd', 'j']
The dataframes look like: 数据帧如下所示:
In [144]: df1
Out[144]:
C1 C2
0 a b
1 b c
2 c x
3 d w
4 e h
5 f j
In [145]: df2
Out[145]:
C1 C2
0 x w
1 a h
2 c d
3 f j
Now, I can use isin to search for whatever I want across all columns of the dataframe. 现在,我可以使用isin在数据框的所有列中搜索所需的内容。
x = (df1[df1.C1.isin(df2.C1) & df1.C2.isin(df2.C2)])
Output: 输出:
C1 C2
5 f j
If you want to search on one column only then, you can remove one of the conditions before or after the &
: 如果仅要搜索一列,则可以删除
&
之前或之后的条件之一:
x = (df1[df1.C1.isin(df2.C1)])
Output: 输出:
C1 C2
0 a b
2 c x
5 f j
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.