pandas is in和not in的使用说明
作者:yyyyyyyyyyang 时间:2023-04-04 22:06:11
简介
pandas按条件筛选数据时,除了使用query()方法,还可以使用isin和对isin取反进行条件筛选.
代码
import pandas as pd
df = pd.DataFrame({'a':[1, 2, 3, 4, 5, 6],
'b':[1, 2, 3, 4, 5, 6],
'c':[1, 2, 3, 4, 5, 6]})
filter_condition = {'a':[1, 2, 3]}
df_in = df[df.isin(filter_condition)['a']]
df_notin = df[~df.isin(filter_condition)['a']]
df.isin(filter_condition)
a b c
0 True True False
1 True True False
2 True False False
3 False False False
4 False False False
5 False False False
使用总结
pandas使用isin时,返回DataFrame中存储的数据为是否满足筛选条件的boolean,可以通过boolean对DataFrame对数据进行筛选。
补充:pandas中isin()函数及其逆函数使用
我使用这个函数就是用来清洗数据,删选过滤掉DataFrame中一些行。
布尔索引
这里你需要知道DateFrame中布尔索引这个东西,可以用满足布尔条件的列值来过滤数据,如下
>>> df=pd.DataFrame(np.random.randn(4,4),columns=['A','B','C','D'])
>>> df
A B C D
0 -0.018330 2.093506 -0.086293 -2.150479
1 0.104931 -0.271810 -0.054599 0.361612
2 0.590216 0.218049 0.157213 0.643540
3 -0.254449 -0.593278 -0.150455 -0.244485
>>> df.A>0#布尔索引
0 False
1 True
2 True
3 False
Name: A, dtype: bool
#布尔索引应用
>>> df[df.A>0]
A B C D
1 0.104931 -0.271810 -0.054599 0.361612
2 0.590216 0.218049 0.157213 0.643540
>>>
isin()
添加一列E
>>> df['E']=['a','a','c','b']
>>> df
A B C D E
0 -0.018330 2.093506 -0.086293 -2.150479 a
1 0.104931 -0.271810 -0.054599 0.361612 a
2 0.590216 0.218049 0.157213 0.643540 c
3 -0.254449 -0.593278 -0.150455 -0.244485 b
>>> df.E.isin(['a','c'])
0 True
1 True
2 True
3 False
Name: E, dtype: bool
>>> df.isin(['b','c'])#整个df也同样适用
A B C D E
0 False False False False False
1 False False False False False
2 False False False False True
3 False False False False True
#应用
>>> df[df.E.isin(['a','c'])]
A B C D E
0 -0.018330 2.093506 -0.086293 -2.150479 a
1 0.104931 -0.271810 -0.054599 0.361612 a
2 0.590216 0.218049 0.157213 0.643540 c
>>>
isin()接受一个列表,判断该列中元素是否在列表中。
同时对多个列过滤,可以如下使用
df[df[某列].isin(条件)&df[某列].isin(条件)]
#应用
>>> df.D=[0,1,0,2]
>>> df[df.E.isin(['a','d'])&df.D.isin([0,])]
A B C D E
0 -0.01833 2.093506 -0.086293 0 a
也可以
不推荐,你试一下就知道
df.isin({
'某列':[条件],
'某列':[条件],
})
#应用
>>> df.D=[0,1,0,2]
>>> df
A B C D E
0 -0.018330 2.093506 -0.086293 0 a
1 0.104931 -0.271810 -0.054599 1 a
2 0.590216 0.218049 0.157213 0 c
3 -0.254449 -0.593278 -0.150455 2 b
>>> df[df.isin({'D':[0,3],'E':['a','d']})]
A B C D E
0 NaN NaN NaN 0.0 a
1 NaN NaN NaN NaN a
2 NaN NaN NaN 0.0 NaN
3 NaN NaN NaN NaN NaN
#没错这不适合选出一行
>>> df.isin({'D':[0,3],'E':['a','d']})
A B C D E
0 False False False True True
1 False False False False True
2 False False False True False
3 False False False False False
isin()的逆函数
告诉你没有isnotin,它的反函数就是在前面加上 ~ ,其他用法同上。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/weixin_42764612/article/details/89600903
标签:pandas,is,in,not,in
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微信小程序 支付功能实现PHP实例详解
2023-11-19 08:53:36
AspJpeg 2.0组件使用教程(GIF篇)
2008-12-16 19:37:00
![](https://img.aspxhome.com/file/UploadPic/200812/16/aspjpeg-10-48.gif)
在ASP中使用Oracle数据库技巧
2008-05-17 11:47:00
Python matplotlib如何简单绘制不同类型的表格
2021-10-16 12:23:48
![](https://img.aspxhome.com/file/2023/7/75887_0s.png)
Python合并多个装饰器小技巧
2022-05-31 04:51:45
Python PyQt4实现QQ抽屉效果
2023-10-08 08:06:42
![](https://img.aspxhome.com/file/2023/6/90446_0s.jpg)
基于Python实现五子棋游戏
2023-01-29 21:47:42
![](https://img.aspxhome.com/file/2023/1/65911_0s.jpg)
基于pip install django失败时的解决方法
2021-06-19 06:01:27
手把手教你搭建python+selenium自动化环境(图文)
2023-03-01 17:45:03
![](https://img.aspxhome.com/file/2023/5/82915_0s.jpg)
Python显示进度条的方法
2023-05-25 07:38:30
![](https://img.aspxhome.com/file/2023/8/103208_0s.png)
如何提升JavaScript的运行速度(递归篇)
2010-05-17 13:30:00
Python .format()函数使用方法详解
2022-10-12 04:18:12
利用python实现汉诺塔游戏
2021-02-19 03:03:45
![](https://img.aspxhome.com/file/2023/3/87783_0s.jpg)
python 读取Linux服务器上的文件方法
2023-03-31 23:54:19
go实现服务优雅关闭的示例
2023-08-06 09:01:08
![](https://img.aspxhome.com/file/2023/7/90487_0s.jpg)
Python+OpenCV实现分水岭分割算法的示例代码
2021-01-11 05:01:38
![](https://img.aspxhome.com/file/2023/7/100827_0s.jpg)
python查看自己安装的所有库并导出的命令
2022-03-01 03:03:08
![](https://img.aspxhome.com/file/2023/8/100938_0s.png)
深入浅析Python2.x和3.x版本的主要区别
2023-06-21 04:12:52
Python自动连接ssh的方法
2023-09-20 00:12:16
Python爬虫小例子——爬取51job发布的工作职位
2022-04-30 23:17:18
![](https://img.aspxhome.com/file/2023/8/101168_0s.jpg)