简体   繁体   中英

Locate rows in one dataframe based on criteria from another dataframe

I have created two Dataframes from a main dataframe, eg df1 and df2.

Each dataframe has the same no of columns, but no of rows in df2 will be less than in df1. The dataframe will have columns waferlot,x,y,w.

How can search for waferlot,x,y,w from df2 in df1.

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 :

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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