日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

python?pandas?query的使用方法_python

作者:soulsoul_god ? 更新時(shí)間: 2022-11-05 編程語(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

欄目分類(lèi)
最近更新