Lemon Bitter
Lemon Bitter

Reputation: 29

User input not working in pandas DataFrame

here is the code!

dff=pd.read_csv('file.csv')

#finding those restaurant which have bar's at night
user=input('enter here')
dff[dff['Collections'].str.contains('user',na=False)]

in this code above the user will enter his choice of restaurant for instance user choose 'Bar', and then the code above will search the KEYWORD 'BAR' in the column name "collections" and return only those dataframe which consists of the keyword Bar in them. but this code is not working its returning me an empty dataframe. am using jupyter notebook

Upvotes: 0

Views: 249

Answers (2)

yibo
yibo

Reputation: 527

Problem

dff[dff['Collections'].str.contains('user', na=False)]

With quote, 'user' is a string in the above line. If nothing in Collections column contains "user" the result would be an empty DataFrame.

Solution

Remove the quote:

dff[dff['Collections'].str.contains(user, na=False)]

Upvotes: 2

bhansa
bhansa

Reputation: 7514

First thing, you have a typo in last line of your code, user is a variable.

Update the last line to:

dff[dff['Collections'].str.contains(user,na=False)]

Another approach:

dff[dff['Collections'] == user]

You might need to check for Case with this approach.

Upvotes: 0

Related Questions