Zaoqi's Blog -> Python数据分析教程 -> 图解Pandas ->
isin - 筛选
isin - 筛选¶
在线刷题
检查 or 强化 Pandas 数据分析操作?👉在线体验「Pandas进阶修炼300题」
Note
本页面代码可以在线编辑、执行!
在 pandas 中有没有类似 SQL 中 IN 和 NOTIN 的筛选方法?
isin 就可以实现,通过 isin 可以快速筛选出包含某个值的结果
根据列表筛选¶
筛选出 country 包含 'China','UK' 的行
import pandas as pd
df3 = pd.DataFrame({'country': ['China','US', 'UK', 'Germany', 'Japan'],
'rank':[1,2,3,4,5]})
df3
| country | rank | |
|---|---|---|
| 0 | China | 1 |
| 1 | US | 2 |
| 2 | UK | 3 |
| 3 | Germany | 4 |
| 4 | Japan | 5 |
df3[df3.country.isin(['China','UK'])]
| country | rank | |
|---|---|---|
| 0 | China | 1 |
| 2 | UK | 3 |
On this page