Al_Mt
Al_Mt

Reputation: 167

How to get the value of what isin return

I'm using .isin() to check if all the values in df1 are also in df2. I'm getting say True 100 and False 1. How can I know which value is this False one?

Upvotes: 1

Views: 1821

Answers (1)

Renaud
Renaud

Reputation: 2819

You can display results with:

import pandas as pd



data1 = {"id":[1,2,3,4,5,6,7,8,9,10],'test':['a','b','c','d','e','f','g','h','i','j']}
data2={"id":[1,2,3,4,5,6,7,8,9,100]}

df1=pd.DataFrame(data1)
df2=pd.DataFrame(data2)

#df1['id'] value is in df2['id']
print("df1['id'] value is in df2['id']")
print(df1[df1['id'].isin(df2['id'])])

print("\n\n\n -------- \n\n\n")


#df1['id'] value isnot in df2['id']
print("df1['id'] value isnot in df2['id']")
print(df1[~df1['id'].isin(df2['id'])])

results:

df1['id'] value is in df2['id']
   id test
0   1    a
1   2    b
2   3    c
3   4    d
4   5    e
5   6    f
6   7    g
7   8    h
8   9    i



 --------



df1['id'] value isnot in df2['id']
     id test
9  10.0    j

Upvotes: 1

Related Questions