Shubham R
Shubham R

Reputation: 7644

isin pandas dataframe from 2 other dataframe

i have a pandas dataframe.

df = pd.DataFrame({'countries':['US','UK','Germany','China','India','Pakistan','lanka'],
               'id':['a','b','c','d','e','f','g']})

also i have two more dataframes. df2 and df3.

df2 = pd.DataFrame({'countries':['Germany','China'],
               'capital':['c','d']})

df3 = pd.DataFrame({'countries':['lanka','USA'],
               'capital':['g','a']})

i want to find the rows in df where df is in df2 and df3

i had this code:

df[df.id.isin(df2.capital)]

but it will find the rows which is in df2.

is there any way i can do it for both df2 and df3 in a single code.

i'e rows from df where df is in df2 and df3

Upvotes: 0

Views: 7613

Answers (1)

jezrael
jezrael

Reputation: 863166

I think you need simply sum both list together:

print (df[df.id.isin(df2.capital.tolist() + df3.capital.tolist())])
  countries id
0        US  a
2   Germany  c
3     China  d
6     lanka  g

Another solution is use numpy.setxor1d - set exclusive-or of two arrays:

print (df[df.id.isin(np.setxor1d(df2.capital, df3.capital))])
  countries id
0        US  a
2   Germany  c
3     China  d
6     lanka  g

Or solution with comment with or - |:

print (df[(df.id.isin(df2.capital)) | (df.id.isin(df3.capital))])
  countries id
0        US  a
2   Germany  c
3     China  d
6     lanka  g

Upvotes: 2

Related Questions