np.where在我的 pandas 上不工作 [英] np.where Not Working in my Pandas

查看:128
本文介绍了np.where在我的 pandas 上不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用pandas时遇到了一个np.where问题,这让我发疯了,我似乎无法通过Google,文档等来解决.

I have an np.where problem using Pandas that is driving me crazy and I can't seem to solve through Google, the documentation, etc.

我希望有人能有洞察力.我确定它并不复杂.

I'm hoping someone has insight. I'm sure it isn't complex.

我有一个df,在其中检查一列中的值-如果该值是'n/a'(作为字符串,而不是.isnull()中的值),则将其更改为另一个值.

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

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

返回:

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

因此该部分有效.

但这:

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

返回:

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

这是怎么回事?

推荐答案

您需要传递布尔掩码和(两个)值列:

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

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)

请参阅 np.where 文档.

或使用 where系列方法:

or alternatively use the where Series method:

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

这篇关于np.where在我的 pandas 上不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆