w3resource

Pandas DataFrame: mask() function

DataFrame - mask() function

The mask() function is used to replace values where the condition is True.

Syntax:

DataFrame.mask(self, cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)

Parameters:

Name Description Type/Default Value Required / Optional
cond      Where cond is False, keep the original value. Where True, replace with corresponding value from other. If cond is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Series/DataFrame (though pandas doesn’t check it). boolean Series/DataFrame, array-like, or callable Required
other       Entries where cond is True are replaced with corresponding value from other. If other is callable, it is computed on the Series/DataFrame and should return scalar or Series/DataFrame. The callable must not change input Series/DataFrame (though pandas doesn’t check it). scalar, Series/DataFrame, or callable Required
inplace  Whether to perform the operation in place on the data. bool
Default Value: False
Required
axis  Alignment axis if needed. int
Default Value: None
Required
level  Alignment level if needed. int
Default Value: None
Required
errors 

Note that currently this parameter won’t affect the results and will always coerce to a suitable dtype.

  • ‘raise’ : allow exceptions to be raised.
  • ‘ignore’ : suppress exceptions. On error return original object.
str, {‘raise’, ‘ignore’}
Default Value: ‘raise’
Required
try_cast Try to cast the result back to the input type (if possible). bool
Default Value: False
Required

Returns: Same type as caller

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - where() function
Next: DataFrame - query() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/dataframe/dataframe-mask.php