问题描述
我正在尝试将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:
编辑
好的,经过一些误会,您仍然可以使用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设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!