3

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?

semblable
  • 773
  • 1
  • 8
  • 26
Dasph
  • 420
  • 2
  • 15
  • Does this answer your question? [How to determine whether a Pandas Column contains a particular value](https://stackoverflow.com/questions/21319929/how-to-determine-whether-a-pandas-column-contains-a-particular-value) – G. Anderson Apr 05 '21 at 22:47
  • Hi @Dasph, welcome to SO. The link above (automatically generated bc Anderson flagged this question as duplicate) answers your question. In your code, `in` is just checking to see if `customer_id` is in the *index* of the series. Check the link for more info. – semblable Apr 05 '21 at 22:53

1 Answers1

3

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
Ade_1
  • 1,480
  • 1
  • 6
  • 17