布尔值掩码上的pandas DataFrame设置值 [英] pandas DataFrame set value on boolean mask

查看:424
本文介绍了布尔值掩码上的pandas DataFrame设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将pandas DataFrame中的许多不同值都设置为相同的值.我以为我了解大熊猫的布尔索引,但没有找到关于此特定错误的任何资源.

I'm trying to set a number of different in a pandas DataFrame all to the same value. I thought I understood boolean indexing for pandas, but I haven't found any resources on this specific error.

import pandas as pd 
df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'f']})
mask = df.isin([1, 3, 12, 'a'])
df[mask] = 30
Traceback (most recent call last):
...
TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value

上面,我想用值30替换掩码中的所有True条目.

Above, I want to replace all of the True entries in the mask with the value 30.

我可以改而做df.replace,但是在这里遮罩感觉更加有效和直观.有人可以解释该错误,并提供一种有效的方法来设置所有值吗?

I could do df.replace instead, but masking feels a bit more efficient and intuitive here. Can someone explain the error, and provide an efficient way to set all of the values?

推荐答案

为此,您不能在混合dtypes上使用布尔掩码,可以使用pandas where设置值:

You can't use the boolean mask on mixed dtypes for this unfortunately, you can use pandas where to set the values:

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)
df

Out[59]:
    A   B
0   1   a
1  30  30
2   3  30

注意:如果您在where方法中执行inplace=True,上述操作将失败,因此df.where(mask, other=30, inplace=True)将引发:

Note: that the above will fail if you do inplace=True in the where method, so df.where(mask, other=30, inplace=True) will raise:

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

TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value

编辑

好的,经过一些误会,您仍然可以使用where y只是将遮罩反转:

OK, after a little misunderstanding you can still use where y just inverting the mask:

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   B
0  30  30
1   2   b
2  30   f

这篇关于布尔值掩码上的pandas DataFrame设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆