網站首頁 編程語言 正文
前言:
Pandas 中應用 query 函數來進行數據篩選。
query 函數的一般用法如下:
df.query('expression')
常用方法:
#!/usr/bin/python
import pandas as pd
import numpy as np
data = {
?'brand':['Python',' C ',' C++ ','C#','Java'],
?'A':[10,2,5,20,16],
?'B':[4,6,8,12,10],
?'C':[8,12,18,8,2],
?'D':[6,18,14,6,12],
?'till years':[4,1,1,30,30]
?}
df = pd.DataFrame(data=data)
print("df數據打印:\n", df, '\n')
print('查找數據:\n', df.query('brand == "Python"'), '\n')
print('查找數據:\n', df[df['brand'] == "Python"], '\n')
可以使用df.query('brand == "Python"')進行查找,也可以使用df[df['brand'] == "Python"]這種方式進行查找。
out:
df數據打印:
? ? ?brand ? A ? B ? C ? D ?till years
0 ?Python ?10 ? 4 ? 8 ? 6 ? ? ? ? ? 4
1 ? ? ?C ? ?2 ? 6 ?12 ?18 ? ? ? ? ? 1
2 ? ?C++ ? ?5 ? 8 ?18 ?14 ? ? ? ? ? 1
3 ? ? ?C# ?20 ?12 ? 8 ? 6 ? ? ? ? ?30
4 ? ?Java ?16 ?10 ? 2 ?12 ? ? ? ? ?30
?
查找數據:
? ? ?brand ? A ?B ?C ?D ?till years
0 ?Python ?10 ?4 ?8 ?6 ? ? ? ? ? 4
?
查找數據:
? ? ?brand ? A ?B ?C ?D ?till years
0 ?Python ?10 ?4 ?8 ?6 ? ? ? ? ? 4
通過數學表達式來篩選:
除了直接通過等于某個值來篩選, query 函數還支持通過數學表達式來進行數據篩選,包括 > 、 < 、 + 、 - 、 * 、 / 等。
print('查找數據:\n', df.query('A > 15'), '\n')
out:
查找數據:
? ?brand ? A ? B ?C ? D ?till years
3 ? ?C# ?20 ?12 ?8 ? 6 ? ? ? ? ?30
4 ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
通過變量篩選:
在程序比較長的時候,經常會使用變量來作為篩選條件, query 函數在使用變量作為判斷標準時,通過在變量前面添加 @ 符號來實現,
示例如下:
name = 'Java'
print('查找數據:\n', df.query('brand == @name'), '\n')
out:
查找數據:
? ?brand ? A ? B ?C ? D ?till years
4 ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
通過列表數據篩選:
當需要在某列中篩選多個符合要求的值的時候,可以通過列表( list )來實現,示例如下:
name = ['Python', 'Java']
print('查找數據:\n', df.query('brand in @name'), '\n')
out:
查找數據:
? ? ?brand ? A ? B ?C ? D ?till years
0 ?Python ?10 ? 4 ?8 ? 6 ? ? ? ? ? 4
4 ? ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
多條件篩選:
- 兩者都需要滿足的并列條件使用符號 & , 或單詞 and
- 只需要滿足其中之一的條件使用符號 | , 或單詞 or
name = ['Python', 'Java']
print('查找數據:\n', df.query('brand in @name & A > 15'), '\n')
out:
查找數據:
? ?brand ? A ? B ?C ? D ?till years
4 ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
列名稱中有空格的情況,使用``進行處理:
使用引號處理的話,會報錯。
print('查找數據:\n', df.query('`till years` > 10'), '\n')
out:
查找數據:
? ?brand ? A ? B ?C ? D ?till years
3 ? ?C# ?20 ?12 ?8 ? 6 ? ? ? ? ?30
4 ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
篩選后選取數據列:
name = ['brand', 'A', 'B', 'till years']
print('查找數據:\n', df.query('`till years` > 10')[name], '\n')
out:
查找數據:
? ?brand ? A ? B ?till years
3 ? ?C# ?20 ?12 ? ? ? ? ?30
4 ?Java ?16 ?10 ? ? ? ? ?30
總結:
當用到多條件篩選時,使用query就會顯得簡潔的多:
print(df[(df['brand'] == 'Python') & (df['A'] == 10) & (df['B'] == 4)])
print(df.query('brand == "Python" & A == 10 & B == 4'))
原文鏈接:https://blog.csdn.net/xiadeliang1111/article/details/126819918
相關推薦
- 2022-03-18 docker?創建容器時指定容器ip的實現示例_docker
- 2022-07-03 python中dict獲取關鍵字與值的實現_python
- 2022-09-23 win11下FTP服務器搭建圖文教程_FTP服務器
- 2021-12-10 linux中的軟連接和硬連接詳解_Linux
- 2022-04-09 SpringBoot自定義validation注解校驗參數只能為指定的值
- 2022-07-16 windows下 使用 nvm-windows 管理node版本
- 2022-06-04 C#?XML基礎入門小結(XML文件內容增刪改查清)_C#教程
- 2023-02-26 React?Fiber原理深入分析_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支