df.mask() 和df.where() 替换方法区别

具体用法详情请看:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.mask.html

现在主要说一下区别:

where 是条件为False才替换,而mask是条件为True才替换

import pandas as pd
s = pd.Series(range(5))
'''
0    0
1    1
2    2
3    3
4    4
dtype: int64
'''
s.where(s>1)
'''
0    NaN
1    NaN
2    2.0
3    3.0
4    4.0
dtype: float64
'''
s.mask(s>1)
'''
0    0.0
1    1.0
2    NaN
3    NaN
4    NaN
dtype: float64
'''
s.where(s>1,10)
'''
0    10
1    10
2     2
3     3
4     4
dtype: int64
'''
s.mask(s>1,10)
'''
0     0
1     1
2    10
3    10
4    10
dtype: int64
'''
原文地址:https://www.cnblogs.com/cgmcoding/p/13589693.html