pandas mask
时间: 2023-08-20 07:07:05 浏览: 126
Pandas `mask` is a method that allows you to selectively replace values in a DataFrame or Series based on a condition. It is similar to the `where` method but with the opposite logic.
The basic syntax of `mask` is as follows:
```
df.mask(cond, other=)
```
Here, `cond` is the condition that determines which values to replace, and `other` is the value to replace with.
The `cond` can be a boolean expression, a callable, or a boolean DataFrame/Series of the same shape as the original DataFrame/Series. If the condition is True, the corresponding value in the DataFrame/Series will be replaced. Otherwise, it will be left unchanged.
If the `other` parameter is not provided, the default value is `NaN`. However, you can specify any other value or a DataFrame/Series of the same shape to replace the values that satisfy the condition.
Here's an example:
```python
import pandas as pd
import numpy as np
data = {'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10]}
df = pd.DataFrame(data)
df_masked = df.mask(df > 3, -1)
print(df_masked)
```
Output:
```
A B
0 1 6
1 2 7
2 3 8
3 -1 -1
4 -1 -1
```
In this example, values greater than 3 in the DataFrame `df` are replaced with -1 using the `mask` method.
阅读全文