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

我有一个使用熊猫的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)

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...