python?pandas?query的使用方法
前言:
Pandas 中應(yīng)用 query 函數(shù)來進(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
通過數(shù)學(xué)表達(dá)式來篩選:
除了直接通過等于某個(gè)值來篩選, query 函數(shù)還支持通過數(shù)學(xué)表達(dá)式來進(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
通過變量篩選:
在程序比較長的時(shí)候,經(jīng)常會(huì)使用變量來作為篩選條件, query 函數(shù)在使用變量作為判斷標(biāo)準(zhǔn)時(shí),通過在變量前面添加 @ 符號(hào)來實(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
通過列表數(shù)據(jù)篩選:
當(dāng)需要在某列中篩選多個(gè)符合要求的值的時(shí)候,可以通過列表( list )來實(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
多條件篩選:
- 兩者都需要滿足的并列條件使用符號(hào) & , 或單詞 and
- 只需要滿足其中之一的條件使用符號(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
列名稱中有空格的情況,使用``進(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ì)顯得簡潔的多:
print(df[(df['brand'] == 'Python') & (df['A'] == 10) & (df['B'] == 4)])
print(df.query('brand == "Python" & A == 10 & B == 4'))到此這篇關(guān)于python pandas query的使用方法的文章就介紹到這了,更多相關(guān)python pandas query 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python Pandas pandas.read_sql_query函數(shù)實(shí)例用法分析
- python中pandas操作apply返回多列的實(shí)現(xiàn)
- python?pandas創(chuàng)建多層索引MultiIndex的6種方式
- Python+pandas編寫命令行腳本操作excel的tips詳情
- Python Pandas實(shí)現(xiàn)DataFrame合并的圖文教程
- Python的Django框架實(shí)現(xiàn)數(shù)據(jù)庫查詢(不返回QuerySet的方法)
- Python中類似于jquery的pyquery庫用法分析
- python實(shí)現(xiàn)合并多個(gè)list及合并多個(gè)django QuerySet的方法示例
相關(guān)文章
這3個(gè)Python實(shí)時(shí)可視化工具包來幫你了解性能瓶頸
由于Python的動(dòng)態(tài)性和多功能性,它比其他語言的速度要慢.有時(shí)復(fù)雜的計(jì)算或算法需要大量時(shí)間才能在Python中執(zhí)行.因此需要跟蹤代碼的執(zhí)行流,深入了解性能瓶頸,需要的朋友可以參考下2021-06-06
Python pandas進(jìn)行數(shù)據(jù)預(yù)處理的實(shí)現(xiàn)
本案例通過使用pandas庫對(duì)電子商務(wù)客戶數(shù)據(jù)進(jìn)行數(shù)據(jù)預(yù)處理,包括數(shù)據(jù)導(dǎo)入、查看、缺失值處理等處理,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
Python turtle實(shí)現(xiàn)貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了Python turtle實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Python requests和httpx實(shí)例詳解
這篇文章主要介紹了Python requests和httpx的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12
Python中使用logging和traceback模塊記錄日志和跟蹤異常
今天小編就為大家分享一篇關(guān)于Python中使用logging和traceback模塊記錄日志和跟蹤異常,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
解決python多行注釋引發(fā)縮進(jìn)錯(cuò)誤的問題
今天小編就為大家分享一篇解決python多行注釋引發(fā)縮進(jìn)錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
對(duì)python opencv 添加文字 cv2.putText 的各參數(shù)介紹
今天小編就為大家分享一篇對(duì)python opencv 添加文字 cv2.putText 的各參數(shù)介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12

