.hwhere和.mask之间有什么区别?我查看了语法和参数,它们看起来非常相似。
这些方法类似,位工作相反。
Series.mask
-替换条件为True的值。
Series.where
-替换条件为False的值。
检查样本数据,对于条件相同的mask
数据,替换值与True
匹配,此处更大类似于4
,对于where
方法,替换数据与False
匹配,此处不更大类似4
,因此小于或等于:
df = pd.DataFrame({
'A':[7,8,9,4,2,3],
})
print(df.A > 4)
0 True
1 True
2 True
3 False
4 False
5 False
Name: A, dtype: bool
df['new1'] = df.A.mask(df.A > 4, 100)
df['new2'] = df.A.where(df.A > 4, 100)
print(df)
A new1 new2
0 7 100 7
1 8 100 8
2 9 100 9
3 4 4 100
4 2 2 100
5 3 3 100