Python 无法找出Pandas.mask()和.where方法之间的区别
where和mask之间有什么区别?我查看了语法和参数,它们看起来非常相似。此方法相似,位工作相反 -如果条件为真,则替换值 -如果条件为假,则替换值 检查样本数据,对于具有相同条件的Python 无法找出Pandas.mask()和.where方法之间的区别,python,pandas,Python,Pandas,where和mask之间有什么区别?我查看了语法和参数,它们看起来非常相似。此方法相似,位工作相反 -如果条件为真,则替换值 -如果条件为假,则替换值 检查样本数据,对于具有相同条件的掩码数据,替换值匹配真,此处更大,如4,对于方法被替换,数据匹配假,此处不大于4,因此小于或等于: df = pd.DataFrame({ 'A':[7,8,9,4,2,3], }) print(df.A > 4) 0 True 1 True 2 True 3
掩码
数据,替换值匹配真
,此处更大,如4
,对于方法被替换,数据匹配假
,此处不大于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
这种方法是相似的,钻头反向工作
-如果条件为真,则替换值
-如果条件为假,则替换值
检查样本数据,对于具有相同条件的掩码
数据,替换值匹配真
,此处更大,如4
,对于方法被替换,数据匹配假
,此处不大于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