Np.where Doesn't Work in My Pandas

I have a problem with np.where using Pandas that drives me crazy and I cannot solve the problem through google, documentation, etc.

I hope someone has an understanding. I am sure it is not difficult.

I have df where I check the value in one column - and if that value is "n / a" (as a string, and not as in .isnull ()), changing it to another value.

Full_Names_Test_2['MarketCap'] == 'n/a'

returns:

 70 True 88 False 90 True 145 True 156 True 181 True 191 True 200 True 219 True 223 False Name: MarketCap, dtype: bool 

for this part to work.

but this:

 Full_Names_Test_2['NewColumn'] = np.where(Full_Names_Test_2['MarketCap'] == 'n/a', 7) 

returns:

 ValueError: either both or neither of x and y should be given 

What's happening?

+5
source share
1 answer

You need to pass the boolean mask and the columns (two) values:

 np.where(Full_Names_Test_2['MarketCap'] == 'n/a', 7) # should be np.where(Full_Names_Test_2['MarketCap'] == 'n/a', Full_Names_Test_2['MarketCap'], 7) 

See np.where .

or alternatively use the where Series method :

 Full_Names_Test_2['MarketCap'].where(Full_Names_Test_2['MarketCap'] == 'n/a', 7) 
+8
source

Source: https://habr.com/ru/post/1234217/


All Articles