pandas where mask函数详解,get操作DataFrame的正确姿势

Pandas是Python数据科学生态中重要的基础成员,功能强大,用法灵活,简单记录之。更佳阅读体验可移步 Pandas核心概述

这里重点介绍pandas的where mask函数,如果能从这两个函数的用法get到pandas的精髓就再好不过了。

用法说明,官方的用法说明比较简洁:
where :替换条件(condition)为Flase处的值
mask :替换条件(condition)为True处的值

where(self, cond, other=nan, inplace=False,
	  axis=None, level=None, errors='' raise', try_cast=False)
	  
mask(self, cond, other=nan, inplace=False,
	  axis=None, level=None, errors='' raise', try_cast=False)

当然,这里的condition 自然就是参数列表中的 cond,既然是替换值。
那么替换后的值是什么呢,就是参数列表中的other ,方法还为other指定了默认值None。

下面先用pandas中的Series对象测试一下:

#定义一个Series
s = pd.Series(range(5))
s
0    0
1    1
2    2
3    3
4    4
dtype: int64

#执行where函数
s.where(s > 2,"我的自定义")
0    我的自定义
1    我的自定义
2    我的自定义
3        3
4        4
dtype: object
#符合条件的显示原来的数据,不符合条件的显示 **other**参数给定的值

#执行mask函数
s.mask(s > 2,"我的自定义")
0        0
1        1
2        2
3    我的自定义
4    我的自定义
dtype: object
#符合条件的显示 **other**参数给定的值,不符合条件的显示原来的数据

上面解释说参数 cond传入布尔值,那么我们来看下s>2,究竟是什么东西

s > 2
0    False
1    False
2    False
3     True
4     True
dtype: bool

以上的操作已经很容易看懂这俩函数的用法,好像这俩函数就是一个相反的函数,在条件一致的情况下得到的结果是相反的,也就是说如果定义m=s>2, 以下两个函数的结果应该是相同的。

m=s>2
s.where(m,"我的自定义")
s.mask(~m ,"我的自定义")

于是以下会得到一组布尔值

s.where(m,"我的自定义")==s.mask(~m ,"我的自定义")
0    True
1    True
2    True
3    True
4    True
dtype: bool

上面我们仅仅是用了自己定义的一个数,如果我们用一组数呢

s.where(s > 2,  pd.Series(range(10,15)))
0    10
1    11
2    12
3     3
4     4
dtype: int64

也成功的替换成了给定Series对应下标的值

那么我们猜测他就是按照下标去对应值,我们换一个长度不一致的Series呢

s.where(s > 2,  pd.Series(range(10,300)))

#结果和上面一致

下面用DataFrame进行测试,验证一下就好了

df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
df
	A	B
0	0	1
1	2	3
2	4	5
3	6	7
4	8	9

测试函数:
条件是 DF中的元素被3除尽 根据函数要求返回-df中的相应值

m=df % 3 == 0
m
 	 A	       B
0	True	False
1	False	True
2	False	False
3	True	False
4	False	True

#where : 不满足条件返回other
df.where(m, -df)
A	B
0	0	-1
1	-2	-3
2	-4	-5
3	6	7
4	8	9

#mask : 满足条件返回other
df.mask(m,-df)
	A	B
0	0	1
1	2	3
2	4	5
3	-6	-7
4	-8	-9

df.where(m, -df) == df.mask(~m, -df)
A	B
0	True	True
1	True	True
2	True	True
3	True	True
4	True	True

官方文中还讲了numpy中另外一个函数的用法与这俩相同

np.where(m, df, -df)
array([[ 0, -1],
       [-2,  3],
       [-4, -5],
       [ 6, -7],
       [-8,  9]])
#输出上好像格式不太一样

df.where(m, -df) == np.where(m, df, -df)
A	B
0	True	True
1	True	True
2	True	True
3	True	True
4	True	True

也能说明pandas和numpy的关系,pansdas是基于Numpy的一种工具

df.where()   
df.mask()  
np.where()  

以上三个函数
上面我们也看到DF的操作其实是非常随心所欲的一个“-df”就把里面所有的元素都取相反数了,一个比较运算符(如 df>2)就可以取所有元素运算的布尔结果,感觉excel是不是弱爆了。

可能会有些啰嗦,但我觉得这样展示出来,可能才会有点收获😂

  • 9
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值