1.前言背景
没怎么用过df.where 都是直接使用loc、apply等方法去解决。
可能是某些功能还没有超出loc和apply的适用范围。
2.进入df.where和df.mask
DataFrame.where(self, cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)
note:Replace values in DataFrame with other where the cond is False.
我们还是要看一下官网对里面每一个参数的解释:
红色是特别注意的,往往无论是博客还是案例一般给不会穷举所有可能,只有把api的每一种可能理解了,才能无招胜有招。
大体意思:就是对一个DataFrame进行条件判断当他的条件不符合就选择other参数里面的数值。
其实它拥有一个相反的函数where<==>mask:where条件不符合进行替换,mask是条件符合进行替换。
DataFrame.mask(self, cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False)
note:Replace values in DataFrame with other where the cond is True.
我们还是要看一下官网对里面每一个参数的解释:
也可以看到两者参数并无差异。