您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:22:40 python 字数 1838 阅读 548 来源 www.jb51.cc/python

我有一个使用熊猫的np.where问题让我发疯,我似乎无法通过谷歌,文档等解决. 我希望有人有洞察力.我敢肯定它并不复杂. 我有一个df我正在检查一列中的值 – 如果该值是’n / a'(作为字符串,而不是.isnull()),将其更改为另一个值. Full_Names_Test_2 [‘MarketCap’] ==’n / a’ 收益: 70 True 88 False 90

概述

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

我有一个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)

总结

以上是编程之家为你收集整理的python – np.where在我的熊猫中不起作用全部内容,希望文章能够帮你解决python – np.where在我的熊猫中不起作用所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶