Python Forum
Getting false even when data exists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Getting false even when data exists (/thread-33688.html)

Pages: 1 2


Getting false even when data exists - sriniyum - May-17-2021

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]


RE: Getting false even when data exists - csr - May-17-2021

What is the exact value you enter, and what is the exact value of df['Customer Key']?


RE: Getting false even when data exists - sriniyum - May-18-2021

(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.


RE: Getting false even when data exists - perfringo - May-18-2021

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.


RE: Getting false even when data exists - sriniyum - May-18-2021

(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.


RE: Getting false even when data exists - perfringo - May-18-2021

(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.


RE: Getting false even when data exists - sriniyum - May-18-2021

(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.


RE: Getting false even when data exists - perfringo - May-18-2021

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



RE: Getting false even when data exists - sriniyum - May-18-2021

(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.


RE: Getting false even when data exists - ibreeden - May-18-2021

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: ")