Dasph
Dasph

Reputation: 446

Pandas "if X not in DataFrame" always throwing false

I have a simple app that prompts for a customer ID. The app uses the input to execute a SQL statement and then converts the resulting set to a dataframe.

I then check if the provided ID exists in the dataframe; however, it always returns false even if the ID does indeed exist. Printing both the input and the dataframe confirms that the ID entered and the ID in the dataframe are the same, and the column name is also correct.

while True:
    customer_id = input("Insert an existing Customer ID")
    print(df)
    print(customer_id)
    customer_exists = customer_id in df.ID
    print(customer_exists)
    if not customer_exists:
        retry = input("No such customer. Try again? Y/N")
        if retry.upper() == 'Y':
            continue
        else:
            return None
    else:
        break

Output:

#The prompt
Insert an existing Customer ID: A1

#The dataframe
   ID  FNAME MNAME    LNAME  ADDRESS1 CUSTOMER_TYPE
0  A1  HELGA  None  PATHAKI  MICHIGAN        BRONZE

#The ID that was input by the user
A1

#the boolean result
False

What is going on here?

Upvotes: 3

Views: 770

Answers (1)

Ade_1
Ade_1

Reputation: 1486

Depending on the volume of data, i would recommend you use the np.isin()

import numpy as np

customer_id = input("Insert an existing Customer ID") #A1
sample= np.isin(customer_id, item2)
print(sample) # returns True

Upvotes: 3

Related Questions