python?pandas?query的使用方法
前言:
Pandas 中應用 query 函數(shù)來進行數(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"')進行查找,也可以使用df[df['brand'] == "Python"]這種方式進行查找。
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ù)學表達式來篩選:
除了直接通過等于某個值來篩選, query 函數(shù)還支持通過數(shù)學表達式來進行數(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
通過變量篩選:
在程序比較長的時候,經(jīng)常會使用變量來作為篩選條件, query 函數(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ù)篩選:
當需要在某列中篩選多個符合要求的值的時候,可以通過列表( list )來實現(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
多條件篩選:
- 兩者都需要滿足的并列條件使用符號 & , 或單詞 and
- 只需要滿足其中之一的條件使用符號 | , 或單詞 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
列名稱中有空格的情況,使用``進行處理:
使用引號處理的話,會報錯。
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
總結:
當用到多條件篩選時,使用query就會顯得簡潔的多:
print(df[(df['brand'] == 'Python') & (df['A'] == 10) & (df['B'] == 4)]) print(df.query('brand == "Python" & A == 10 & B == 4'))
到此這篇關于python pandas query的使用方法的文章就介紹到這了,更多相關python pandas query 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Python Pandas pandas.read_sql_query函數(shù)實例用法分析
- python中pandas操作apply返回多列的實現(xiàn)
- python?pandas創(chuàng)建多層索引MultiIndex的6種方式
- Python+pandas編寫命令行腳本操作excel的tips詳情
- Python Pandas實現(xiàn)DataFrame合并的圖文教程
- Python的Django框架實現(xiàn)數(shù)據(jù)庫查詢(不返回QuerySet的方法)
- Python中類似于jquery的pyquery庫用法分析
- python實現(xiàn)合并多個list及合并多個django QuerySet的方法示例
相關文章
Python實現(xiàn)動態(tài)給類和對象添加屬性和方法操作示例
這篇文章主要介紹了Python實現(xiàn)動態(tài)給類和對象添加屬性和方法操作,涉及Python面向對象程序設計中類與對象屬性、方法的動態(tài)操作相關實現(xiàn)技巧,需要的朋友可以參考下2020-02-02Django uwsgi Nginx 的生產環(huán)境部署詳解
這篇文章主要介紹了Django uwsgi Nginx 的生產環(huán)境部署詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02python導出requirements.txt的幾種方法以及環(huán)境配置詳細流程
這篇文章主要給大家介紹了關于python導出requirements.txt的幾種方法以及環(huán)境配置詳細流程,requirements.txt 文件是一個文本文件,用于列出你的Python項目所依賴的軟件包及其版本,需要的朋友可以參考下2023-11-11