Skip to content
Advertisement

Read data from Excel and search it in df, TypeError: ‘in ‘ requires string as left operand, not float

I read a lot about this Error, but I couldn’t find a solution for me.

I have an Excel with 3 columns in which I store keywords. I want to read these keywords and search it in a Pandas Dataframe. The Code below gives me an Error:

JavaScript

The Code:

JavaScript

But when I read just one column from Excel and write the another Keywords in the Code, it works fine: (I have more Keywords in EKN and EDT, it’s just to show my problem)

JavaScript

The ouput of print(Keywords_EKN[y]) is

JavaScript

I don’t know, what’s the problem. Thanks for any help.

Advertisement

Answer

Your EKN contains np.nan which is float value (or any other non-string value). You can invoke the error with code like this:

JavaScript

Result is error because in expects string from kw[y] but got float. Solution could be quite simple:

JavaScript

or in your case:

JavaScript

or replace nan values from the dataframe at the beginning as suggested Timus in comment.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement