我试图将pandas DataFrame中的多个不同值全部设置为相同的值。我以为我理解了pandas的布尔索引,但是我没有找到任何关于这个特定错误的资源。
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
上面,我想将遮罩中所有的True
替换为值30
。
我可以使用df.replace
进行替换,但在这里使用遮罩感觉更有效和直观。有人能解释一下错误,并提供一种高效的方法来设置所有的值吗?