mars.dataframe.DataFrame.mask#
- DataFrame.mask(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)#
在条件为真时替换值。
- Parameters
cond (布尔系列/数据框, 类似数组, 或 可调用对象) – 当 cond 为 False 时,保留原始值。当为 True 时,用 other 中对应的值替换。 如果 cond 是可调用对象,它将在系列/数据框上计算,并应返回布尔系列/数据框或数组。可调用对象必须不更改输入系列/数据框(尽管 pandas 不会检查这一点)。
other (标量, 系列/数据框, 或者可调用) – 当 cond 为真时,条目将被替换为来自 other 的相应值。 如果 other 是可调用的,它将在 Series/DataFrame 上计算,并且应该返回标量或 Series/DataFrame。可调用函数不得更改输入的 Series/DataFrame(尽管 pandas 并不检查这一点)。
inplace (bool, default False) – 是否在数据上执行就地操作。
axis (int, 默认 None) – 如果需要,设置对齐轴。
level (int, 默认 None) – 如果需要的对齐等级。
错误 (字符串, {'引发', '忽略'}, 默认 '引发') –
请注意,目前此参数不会影响结果,并将始终强制转换为合适的数据类型。
’引发’ : 允许引发异常。
’忽略’ : 抑制异常。在出错时返回原始对象。
try_cast (bool, 默认为 False) – 尝试将结果转换回输入类型(如果可能)。
- Return type
与调用者相同的类型
另请参阅
DataFrame.where()返回与自身形状相同的对象。
备注
掩码方法是 if-then 模式的应用。对于调用 DataFrame 中的每个元素,如果
cond为False,则使用该元素;否则使用 DataFrameother中对应的元素。DataFrame.where()的签名与numpy.where()不同。大致上df1.where(m, df2)相当于np.where(m, df1, df2)。有关更多详细信息和示例,请参阅indexing中的
mask文档。示例
>>> import mars.tensor as mt >>> import mars.dataframe as md >>> s = md.Series(range(5)) >>> s.where(s > 0).execute() 0 NaN 1 1.0 2 2.0 3 3.0 4 4.0 dtype: float64
>>> s.mask(s > 0).execute() 0 0.0 1 NaN 2 NaN 3 NaN 4 NaN dtype: float64
>>> s.where(s > 1, 10).execute() 0 10 1 10 2 2 3 3 4 4 dtype: int64
>>> df = md.DataFrame(mt.arange(10).reshape(-1, 2), columns=['A', 'B']) >>> df.execute() A B 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9 >>> m = df % 3 == 0 >>> df.where(m, -df).execute() A B 0 0 -1 1 -2 3 2 -4 -5 3 6 -7 4 -8 9 >>> df.where(m, -df) == mt.where(m, df, -df).execute() A B 0 True True 1 True True 2 True True 3 True True 4 True True >>> df.where(m, -df) == df.mask(~m, -df).execute() A B 0 True True 1 True True 2 True True 3 True True 4 True True