Python Forum
Comparing Dataframe to String?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing Dataframe to String?
#1
I'm trying to learn how to use Pandas to manipulate csv's and excel files.

One thing I'm trying to do is read a list of colors from a csv list, then ask the user for a color input.

Then I want to compare the user input to the data read from the color csv list.

The problem I have is when I type in a color I know that is on the list, like say "Aqua" or "Yellow" for example, I will get the message "This value is not on the list."

Is there some sort of type conversion or other comparison I need to be doing?

Code for reference.

import pandas as pd
df = pd.read_csv('color_list_csv.csv', header = None)

print(df)

User_Color_Input = input("Please enter a Color: ")

if 'User_Color_Input' in df.values :
    print("\nThis value exists in Dataframe")
 else :
    print("\nThis value does not exist in Dataframe")
df.to_excel('color_list_output.xlsx', index = False)
This is the color list I'm using for reference, its a single column in csv file.

Reply
#2
if 'User_Color_Input' in df.values :
here you check that string 'User_Color_Input' is present in the df values

if User_Color_Input in df.values :
RockBlok likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
That did it, haha.

Thanks for the help *slaps forehead*
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Dataframe: comparing value in last row vs the row before last lorensa74 2 2,686 Jul-08-2021, 04:51 PM
Last Post: jefsummers
  String Comparing Harshil 15 3,987 Aug-05-2020, 05:50 PM
Last Post: Harshil
  Create a new list by comparing values in a list and string DBS 2 3,540 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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