Home > Net >  Get the panda where function to ignore the blank values in a column?
Get the panda where function to ignore the blank values in a column?

Time:04-26

The following code is not working the way I expect.

dfe['Activity_2'] = np.where(dfe['ZBPCENTY'].str.contains("3324"), "Yes","no")

I am checking to see if the text "3324" appears anywhere in the ZBPCENTY column. If so, I want to print "Yes" in the Activity_2 column. If no, print "No"

The issue is the code prints "Yes" in all blank columns too.

All data are in text format.

How can I fix this? It seems so simple, but I can't figure out what I am doing wrong.

CodePudding user response:

Does it work if you try the same statement but with parameter 'na=False' ? According to the docs, it replaces NaN values with False

So that would be:

dfe['Activity_2'] = np.where(dfe['ZBPCENTY'].str.contains("3324",na=False), "Yes","no")

CodePudding user response:

I figured it out. I added "== True" after the ('3324')

  • Related