假设我有一个数据帧all_data
,如下所示:
Id Zone Neighb
1 NaN IDOTRR
2 RL Veenker
3 NaN IDOTRR
4 RM Crawfor
5 NaN Mitchel
我想在“Zone”列中输入缺少的值,这样当“Neighb”是“IDOTRR”时,我将“Zone”设置为“RM”,而其中“Neighb”是“Mitchel”,我设置为“RL”。在
^{pr2}$我得到:
TypeError: invalid type comparison
C:\Users\pprun\Anaconda3\lib\site-packages\pandas\core\ops.py:798: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
result = getattr(x, name)(y)
我肯定这应该很简单,但我已经在这上面混了太久了。请帮忙。在
在Python中,
&
优先于==
http://www.annedawson.net/Python_Precedence.htm
所以当你做
all_data.MSZoning.isnull() & all_data.Neighborhood == "Mitchel"
时,这被解释为(all_data.MSZoning.isnull() & all_data.Neighborhood) == "Mitchel"
,现在Python尝试AND
一个带有str序列的布尔序列,看看它是否等于一个str"Mitchel"
。解决方案是将测试放在括号中:(all_data.MSZoning.isnull()) & (all_data.Neighborhood == "Mitchel")
。有时,如果我有很多选择器,我会将它们分配给变量,然后AND
它们,例如:这不仅解决了操作顺序问题,还意味着
all_data.loc[null_zoning & Mitchel_neighb, "MSZoning"] = "RL"
适合一行。在使用np.选择i、 e
^{pr2}$在你的情况下,你可以使用
相关问题 更多 >
编程相关推荐