Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas can't find a value in dataframe using values in another dataframe

I have two dataframes:

df1

Index geoid10 precinct_2020
1 360050020002003 43
2 360610005001008 1
3 360610008006013 5
4 360610151003000 20
5 360610241002002 33
6 360050255001000 52
7 360470002001014 72

df2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Index geoid10 precinct_2020 population
0 360610005001008 750 0
1 360610008006013 750 0
2 360610241002002 750 0
3 360050255001000 750 1990
4 360470002001014 750 333

As you can see, the ‘geoid10’ columns in df1 and df2 have matching values. However, when I try to search for each value of df1’s geoid10 column inside df2’s geoid10 column, my code returns "false". I have made sure that both ‘geoid10’ columns are ints. Why is this happening?

Here is the sample for loop:

for gid in df1['geoid10']: 
  if gid in df2['geoid10']: 
    print("true")
  else: 
    print("false")

Output:

false
false
false
false
false
false
false
false

>Solution :

You can use pandas built-in function isin:

print(df1['geoid10'].isin(df2["geoid10"]))

Output:

0    False
1     True
2     True
3    False
4     True
5     True
6     True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading