栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

布尔值掩码上的pandas DataFrame设置值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

布尔值掩码上的pandas DataFrame设置值

不幸的是,不能在混合dtypes上使用布尔型掩码,可以使用pandas

where
设置值:

In [59]:df = pd.Dataframe({'A': [1, 2, 3], 'B': ['a', 'b', 'f']})mask = df.isin([1, 3, 12, 'a'])df = df.where(mask, other=30)dfOut[59]:    A   B0   1   a1  30  302   3  30

注意:如果您

inplace=True
使用该
where
方法,上述操作将失败,因此
df.where(mask, other=30,inplace=True)
会引发:

TypeError:无法对具有非np.nan值的混合类型进行就地布尔设置

编辑

好的,经过一些误会,您仍然可以使用

where
y,只需反转遮罩即可:

In [2]:    df = pd.Dataframe({'A': [1, 2, 3], 'B': ['a', 'b', 'f']})mask = df.isin([1, 3, 12, 'a'])df.where(~mask, other=30)Out[2]:    A   B0  30  301   2   b2  30   f


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661354.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号