Pandas isin() returning all false

Viewed 1816

I'm using pandas 1.1.3, the latest available with Anaconda.

I have two DataFrames, imported from a .txt and a .xlsx file. They have a column called "ID" which is an int64 (verified with df.info()) on both DataFrames.

df1:

          ID        Name
0 1234564567 Last, First
1 1234564569 Last, First
...

df2:

          ID Amount
0 1234564567  59.99
1 5678995545  19.99

I want to check if all of the IDs on df1 are on df2. For this I create a series:

foo = df1["ID"].isin(df2["ID"])

And I get that all values are False, even though manually I checked and the values do match.

0       False
1       False
2       False
3       False
4       False 
...

I'm not sure if I'm missing something, if there is something wrong with the environment, or if it is a known bug.

2 Answers

You must do something wrong. Try to reproduce this error with a toy example as I did here. The below works for me.

Reproducing with and sharing a minimal example not only allows you to challenge your error but also allows us to provide help.

import pandas as pd
import numpy as np
data = {'Name':['Tom', 'nick'], 'ID':[1234564567, 1234564569]} 
data2 = {'Name':['Tom', 'nick'], 'ID':[1234564567, 5678995545]} 
# Create DataFrame 
df = pd.DataFrame(data) 
df2 = pd.DataFrame(data2) 
df["ID"].isin(df2["ID"])
0     True
1    False
Name: ID, dtype: bool

EDIT: with Paul's data I don't get any error. See the importance of providing examples?

import pandas as pd
data = {'ID':['1234564567', '1234564569'],'Name':['Last, First', 'Last, First']}
data2 = {'ID':['1234564567', '5678995545'],'Amount': [59.99, 19.99]}
df1 = pd.DataFrame(data)
df2 = pd.DataFrame(data2)
df["ID"].isin(df2["ID"])
0     True
1    False
import pandas as pd
data = {'ID':['1234564567', '1234564569'],'Name':['Last, First', 'Last, First']}
data2 = {'ID':['1234564567', '5678995545'],'Amount': [59.99, 19.99]}
df1 = pd.DataFrame(data)
df2 = pd.DataFrame(data2)

now we have that set up we get to the meat...

df1["ID"].apply(lambda x: df2['ID'].isin([x]))

Which shows

    0       1
0   True    False
1   False   False

That ID 0 in df1 is in ID 0 of df2

Related