加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – np.where在我的熊猫中不起作用

发布时间:2020-12-16 23:17:40 所属栏目:Python 来源:网络整理
导读:我有一个使用熊猫的np.where问题让我发疯,我似乎无法通过谷歌,文档等解决. 我希望有人有洞察力.我敢肯定它并不复杂. 我有一个df我正在检查一列中的值 – 如果该值是’n / a'(作为字符串,而不是.isnull()),将其更改为另一个值. Full_Names_Test_2 [‘MarketCa
我有一个使用熊猫的np.where问题让我发疯,我似乎无法通过谷歌,文档等解决.

我希望有人有洞察力.我敢肯定它并不复杂.

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

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

到底是怎么回事?

解决方法

您需要传递布尔掩码和(两个)值列:
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文档.

或者使用the where Series method:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读