0

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

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
quokka
  • 13
  • 3

2 Answers2

1

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
Tranbi
  • 11,407
  • 6
  • 16
  • 33
0

You have to create a list of the second dataframe in order to check if the interated gid is in the second dataframe.

for gid in df1["geoid10"]:
    if gid in list(df2["geoid10"]):
        print("true")
    else:
        print("false")
Maik Hasler
  • 1,064
  • 6
  • 36
  • I figured it was something so simple. If I did not create the list, what was my code searching? – quokka Dec 03 '21 at 07:20
  • @quokka Actually I have no idea, what you were searching for exactly . – Maik Hasler Dec 03 '21 at 07:21
  • df2 has a column with data that I need to populate into df1 and I needed to search df2 via the 'geoid10' index, but df2 would have been too large for me to just merge/concatenate the columns. thank you for your help! – quokka Dec 03 '21 at 07:26