Python Forum
Getting false even when data exists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting false even when data exists
#1
I have a csv file in pandas Data Frame and while trying to check for existence of Customer Key, it gives me False even when the Customer key provided by user is valid and exists in the Data frame.
CustomerKey = input("enter the Customer Key: ")
if CustomerKey in df['Customer Key']:
        print('True')
else:
    print('False')
the csv file has [660 rows x 7 columns]
buran write May-17-2021, 04:37 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
What is the exact value you enter, and what is the exact value of df['Customer Key']?
buran likes this post
Reply
#3
(May-17-2021, 08:17 AM)csr Wrote: What is the exact value you enter, and what is the exact value of df['Customer Key']?

df['Customer Key'] will have all the values of the Customer Key from the csv file. User will input(enter) Customer Key or any number and it should be checked in the existing data of Customer Key. for valid i.e value exists in Customer Key print shud be True else it shud be false.
Reply
#4
What about datatypes? User input by default is string. What type of data is in dataframe column? You are using word “number” which indicates that it could be integer. If this is the case then obviously no matches will be found.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(May-18-2021, 03:50 AM)perfringo Wrote: What about datatypes? User input by default is string. What type of data is in dataframe column? You are using word “number” which indicates that it could be integer. If this is the case then obviously no matches will be found.

how do i set the user input (string) to integer ?
either it is printing endless loop or giving false value when tried with For loop. please help. i am new to this python and having very difficult in getting this executed.
Reply
#6
(May-18-2021, 11:00 AM)sriniyum Wrote: how do i set the user input (string) to integer ?

In it's simplest form:

>>> from_user = input('Enter integer: ')
Enter integer: 5
>>> type(from_user)
<class 'str'>
>>> from_user = int(input('Enter integer: '))
Enter integer: 5
>>> type(from_user)
<class 'int'>
However, this will throw error if user enters something which is not convertible to integer:

>>> from_user = int(input('Enter integer: '))
Enter integer: one
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'one'
You should use try..except for catching error. If this needed to be adressed you could look for 'user input validation' in this forum earlier threads.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(May-18-2021, 01:36 PM)perfringo Wrote:
(May-18-2021, 11:00 AM)sriniyum Wrote: how do i set the user input (string) to integer ?

In it's simplest form:

>>> from_user = input('Enter integer: ')
Enter integer: 5
>>> type(from_user)
<class 'str'>
>>> from_user = int(input('Enter integer: '))
Enter integer: 5
>>> type(from_user)
<class 'int'>
However, this will throw error if user enters something which is not convertible to integer:

>>> from_user = int(input('Enter integer: '))
Enter integer: one
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'one'
You should use try..except for catching error. If this needed to be adressed you could look for 'user input validation' in this forum earlier threads.

But here in this loc

CustomerKey = input("enter the Customer Key: ")

what i am taking from user is integer only while testing to verify the existence of customer key in csv file still not working.
Reply
#8
Hmm.... 'Is not working'....

Quote:"… There's no more work. We're destitute. I'm afraid I have no choice but to sell you all for scientific experiments."

–The Meaning of Life

One should be specific while looking for help. I for example don't understand the problem/question. I have no idea how data from csv is read into dataframe etc. Therefore I repeat my earlier answer with some additional lines:

>>> df = pd.DataFrame(range(1, 4))
>>> df
   0
0  1
1  2
2  3
>>> from_user = input('Enter integer: ')
Enter integer: 1
>>> from_user in df[0]
False
>>> from_user = int(input('Enter integer: '))
Enter integer: 1
>>> from_user in df[0]
True
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
(May-18-2021, 01:59 PM)perfringo Wrote: Hmm.... 'Is not working'....

Quote:"… There's no more work. We're destitute. I'm afraid I have no choice but to sell you all for scientific experiments."

–The Meaning of Life

One should be specific while looking for help. I for example don't understand the problem/question. I have no idea how data from csv is read into dataframe etc. Therefore I repeat my earlier answer with some additional lines:

>>> df = pd.DataFrame(range(1, 4))
>>> df
   0
0  1
1  2
2  3
>>> from_user = input('Enter integer: ')
Enter integer: 1
>>> from_user in df[0]
False
>>> from_user = int(input('Enter integer: '))
Enter integer: 1
>>> from_user in df[0]
True

The csv data has the below Columns(details)
Customer Key,Avg_Credit_Limit,Total_Credit_Cards,Total_visits_bank,Total_visits_online,Total_calls_made

loaded data in pandas dataframe and executing to verify the existence of customer key in the dataframe from user entered value and to print true if customer key exists else print false. hope this give more idea to you.
Reply
#10
But have you tried what Perfringo suggested? In what you show us you still don't convert the user input to integer.
(May-18-2021, 01:44 PM)sriniyum Wrote: CustomerKey = input("enter the Customer Key: ")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 532 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 852 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  How to insert data if not exists in mysql? farah97 0 1,174 Dec-29-2019, 08:32 AM
Last Post: farah97

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020