根据另一列Pandas值的更改

2024-10-01 11:22:06 发布

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

假设我有一个数据帧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)

我肯定这应该很简单,但我已经在这上面混了太久了。请帮忙。在


Tags: 数据rmidzonedatananallcomparison
3条回答
df.Zone=df.Zone.fillna(df.Neighb.replace({'IDOTRR':'RM','Mitchel':'RL'}))
df
Out[784]: 
   Id Zone   Neighb
0   1   RM   IDOTRR
1   2   RL  Veenker
2   3   RM   IDOTRR
3   4   RM  Crawfor
4   5   RL  Mitchel

在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它们,例如:

null_zoning = all_data.MSZoning.isnull()
Mitchel_neighb = all_data.Neighborhood == "Mitchel"
all_data.loc[null_zoning & Mitchel_neighb, "MSZoning"] = "RL"

这不仅解决了操作顺序问题,还意味着all_data.loc[null_zoning & Mitchel_neighb, "MSZoning"] = "RL"适合一行。在

使用np.选择i、 e

df['Zone'] = np.select([df['Neighb'] == 'IDOTRR',df['Neighb'] == 'Mitchel'],['RM','RL'],df['Zone'])
^{pr2}$

在你的情况下,你可以使用

# Boolean mask of condition 1 
m1 = (all_data.MSZoning.isnull()) & (all_data.Neighborhood == "IDOTRR")
# Boolean mask of condition 2
m2 = (all_data.MSZoning.isnull()) & (all_data.Neighborhood == "Mitchel")

np.select([m1,m2],['RM','RL'],all_data["MSZoning"])

相关问题 更多 >