Posts: 6
Threads: 1
Joined: May 2021
May-17-2021, 04:14 AM
(This post was last modified: May-17-2021, 04:37 AM by buran.)
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.
Posts: 4
Threads: 0
Joined: May 2021
What is the exact value you enter, and what is the exact value of df['Customer Key'] ?
Posts: 6
Threads: 1
Joined: May 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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 6
Threads: 1
Joined: May 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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
May-18-2021, 01:36 PM
(This post was last modified: May-18-2021, 01:36 PM by perfringo.)
(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.
Posts: 6
Threads: 1
Joined: May 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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 6
Threads: 1
Joined: May 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.
Posts: 582
Threads: 1
Joined: Aug 2019
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: ")
|