Skip to content
Advertisement

Trouble when adding values for NaN in DataFrame

I have this DataFrame:

JavaScript

And I want to fill the NaN values with keyword taken from the description. To that end I created a list with the keywords I want:

JavaScript

Finally, I want to loop over each row in the DataFrame. Split the contents from the “description” column in each row and, if that word is also in the “keyword” list, add it in the “manufacturer” column. As an example, it would look like this:

JavaScript

Thanks to someone in this community I could improve my code to this:

JavaScript

But I realized that the first row also changed values even though it was not NaN:

JavaScript

I would like to only change the NaN values but when I try to add:

JavaScript

It doesn’t have any effect.

Advertisement

Answer

np.nan == np.nan is False. A bit counter-intuitive perhaps =) But it should mean that the last conditional should never kick in. Not really clear from your question whether you see the same result or no result.

If you changed

JavaScript

to

JavaScript

then I think it should work fine. You would only get the rows in which ‘manufacturer’ is NaN. You could also delete the == np.nan part since non-empty strings evaluate to True and np.nan evaluates to False but that would make your code harder to understand.

There a lot of ways in which your code could look nicer ;) but focus on learning to debug and the rest will come. And as long as it does what you want it to do who cares.

One way you could have debugged this would have been to print the truth value of each part of your conditional inside the loop.

JavaScript

Best wishes!

Edit: okay, I should probably add how I would do this myself.

JavaScript

Not sure if you want the last or first item in keywords when both appear in the string tho. I set it to the first item here using index 0.

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