不在我的Pandas馆工作的地方

2024-09-27 19:22:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个np。在那里使用熊猫的问题让我疯狂,我似乎无法通过谷歌,文档等解决

我希望有人有洞察力。我相信这并不复杂。

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

怎么回事?


Tags: 字符串name文档testfalsetruedfnames
1条回答
网友
1楼 · 发布于 2024-09-27 19:22:19

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

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)

参见^{}文档。

或者使用the ^{} Series method

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

相关问题 更多 >

    热门问题