網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言:
Pandas 中應(yīng)用 query 函數(shù)來(lái)進(jìn)行數(shù)據(jù)篩選。
query 函數(shù)的一般用法如下:
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數(shù)據(jù)打印:\n", df, '\n')
print('查找數(shù)據(jù):\n', df.query('brand == "Python"'), '\n')
print('查找數(shù)據(jù):\n', df[df['brand'] == "Python"], '\n')
可以使用df.query('brand == "Python"')進(jìn)行查找,也可以使用df[df['brand'] == "Python"]這種方式進(jìn)行查找。
out:
df數(shù)據(jù)打印:
? ? ?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
?
查找數(shù)據(jù):
? ? ?brand ? A ?B ?C ?D ?till years
0 ?Python ?10 ?4 ?8 ?6 ? ? ? ? ? 4
?
查找數(shù)據(jù):
? ? ?brand ? A ?B ?C ?D ?till years
0 ?Python ?10 ?4 ?8 ?6 ? ? ? ? ? 4
通過(guò)數(shù)學(xué)表達(dá)式來(lái)篩選:
除了直接通過(guò)等于某個(gè)值來(lái)篩選, query 函數(shù)還支持通過(guò)數(shù)學(xué)表達(dá)式來(lái)進(jìn)行數(shù)據(jù)篩選,包括 > 、 < 、 + 、 - 、 * 、 / 等。
print('查找數(shù)據(jù):\n', df.query('A > 15'), '\n')
out:
查找數(shù)據(jù):
? ?brand ? A ? B ?C ? D ?till years
3 ? ?C# ?20 ?12 ?8 ? 6 ? ? ? ? ?30
4 ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
通過(guò)變量篩選:
在程序比較長(zhǎng)的時(shí)候,經(jīng)常會(huì)使用變量來(lái)作為篩選條件, query 函數(shù)在使用變量作為判斷標(biāo)準(zhǔn)時(shí),通過(guò)在變量前面添加 @ 符號(hào)來(lái)實(shí)現(xiàn),
示例如下:
name = 'Java'
print('查找數(shù)據(jù):\n', df.query('brand == @name'), '\n')
out:
查找數(shù)據(jù):
? ?brand ? A ? B ?C ? D ?till years
4 ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
通過(guò)列表數(shù)據(jù)篩選:
當(dāng)需要在某列中篩選多個(gè)符合要求的值的時(shí)候,可以通過(guò)列表( list )來(lái)實(shí)現(xiàn),示例如下:
name = ['Python', 'Java']
print('查找數(shù)據(jù):\n', df.query('brand in @name'), '\n')
out:
查找數(shù)據(jù):
? ? ?brand ? A ? B ?C ? D ?till years
0 ?Python ?10 ? 4 ?8 ? 6 ? ? ? ? ? 4
4 ? ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
多條件篩選:
- 兩者都需要滿(mǎn)足的并列條件使用符號(hào) & , 或單詞 and
- 只需要滿(mǎn)足其中之一的條件使用符號(hào) | , 或單詞 or
name = ['Python', 'Java']
print('查找數(shù)據(jù):\n', df.query('brand in @name & A > 15'), '\n')
out:
查找數(shù)據(jù):
? ?brand ? A ? B ?C ? D ?till years
4 ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
列名稱(chēng)中有空格的情況,使用``進(jìn)行處理:
使用引號(hào)處理的話,會(huì)報(bào)錯(cuò)。
print('查找數(shù)據(jù):\n', df.query('`till years` > 10'), '\n')
out:
查找數(shù)據(jù):
? ?brand ? A ? B ?C ? D ?till years
3 ? ?C# ?20 ?12 ?8 ? 6 ? ? ? ? ?30
4 ?Java ?16 ?10 ?2 ?12 ? ? ? ? ?30
篩選后選取數(shù)據(jù)列:
name = ['brand', 'A', 'B', 'till years']
print('查找數(shù)據(jù):\n', df.query('`till years` > 10')[name], '\n')
out:
查找數(shù)據(jù):
? ?brand ? A ? B ?till years
3 ? ?C# ?20 ?12 ? ? ? ? ?30
4 ?Java ?16 ?10 ? ? ? ? ?30
總結(jié):
當(dāng)用到多條件篩選時(shí),使用query就會(huì)顯得簡(jiǎn)潔的多:
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
相關(guān)推薦
- 2023-04-12 python的去重以及數(shù)據(jù)合并的用法說(shuō)明_python
- 2022-08-20 python數(shù)字圖像處理之對(duì)比度與亮度調(diào)整示例_python
- 2022-12-14 C++?容器中map和unordered?map區(qū)別詳解_C 語(yǔ)言
- 2022-04-19 運(yùn)行 npm run xxx 的時(shí)候都執(zhí)行了些什么
- 2023-06-21 ProtoBuf動(dòng)態(tài)拆分Gradle?Module解析_Android
- 2022-01-16 npm:使用npm link來(lái)調(diào)試本地的包
- 2022-04-11 Python利用正則表達(dá)式從字符串提取數(shù)字_python
- 2022-07-08 C#跨PC遠(yuǎn)程調(diào)用程序并顯示UI界面_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支