程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[dataframe related functions of pandas] query() function

編輯:Python

**DataFrame.query(expr, inplace=False, **kwargs)[source]
Query the columns of a DataFrame with a boolean expression.
Query by Boolean expression
**

Preview of files to be read :

import pandas as pd
# Read excel
file_path = r'C:\Users\123\Desktop\pandastest\example.xlsx'
df = pd.read_excel(file_path, sheet_name=0, header=0, skipfooter=0)
df1 = df.copy()
print(df)
# Preview original dataframe
# A. Query in each row , The price is greater than 100 The line of .
print(" Query in each row , The price is greater than 100 The line of .")
print(df1.query(' Price > 100 '))
# A. Query title , The value is equal to " longuette "," shoes " The line of 
print(" Query title , The value is equal to the long skirt , Shoe line ")
print(df.query(' title in [" longuette "," shoes "]'))
# A. Query title , Value inclusion “ skirt ” The line of 
print(" Query title , Value inclusion “ skirt ” The line of ")
print(df.query(' title .str.contains(" skirt ")'))
# B. Query lines with the same number as the purchase quantity .
print(" Query lines with the same number as the purchase quantity .")
print(df1.query(' Number == Purchase quantity '))
# B. Check the price * The purchase quantity is greater than 1000 The line of .
print(" Check the price * The purchase quantity is greater than 1000 The line of .")
print(df1.query(' Price * Purchase quantity > 1000 '))
# Multiple criteria query & and and
print(" Multiple criteria query & and and")
print(df1.query(' ( Price <100) & ( Price > 30.0)'))
# Passing in variables @
print(" Passing in variables @")
aver_price = df[' Price '].mean()
print(df1.query(' Price > @aver_price '))

Running results :

 Query in each row , The price is greater than 100 The line of .
Number title Price Purchase quantity Refund status Order creation time Order payment time
2 3 longuette 999.0 1 Refunded 2022-03-31 21:54:46 2022-03-31 21:54:54
5 6 T T-shirt 999.0 1 Refunded 2022-03-31 16:20:27 2022-03-31 16:20:33
7 8 longuette 999.0 5 Refunded 2022-03-31 11:13:04 2022-03-31 11:14:32
9 10 T T-shirt 999.0 2 No refund requested 2022-03-29 23:22:42 2022-03-29 23:22:46
12 13 shoes 999.0 1 Refunded 2022-03-29 13:28:01 2022-03-29 13:28:13
Query title , The value is equal to the long skirt , Shoe line
Number title Price Purchase quantity Refund status Order creation time Order payment time
2 3 longuette 999.0 1 Refunded 2022-03-31 21:54:46 2022-03-31 21:54:54
7 8 longuette 999.0 5 Refunded 2022-03-31 11:13:04 2022-03-31 11:14:32
11 12 shoes 29.9 1 No refund requested 2022-03-29 13:28:01 2022-03-29 13:28:13
12 13 shoes 999.0 1 Refunded 2022-03-29 13:28:01 2022-03-29 13:28:13
13 14 shoes 39.9 1 No refund requested 2022-03-29 11:06:52 2022-03-29 11:06:57
Query title , Value inclusion “ skirt ” The line of
Number title Price Purchase quantity Refund status Order creation time Order payment time
2 3 longuette 999.0 1 Refunded 2022-03-31 21:54:46 2022-03-31 21:54:54
7 8 longuette 999.0 5 Refunded 2022-03-31 11:13:04 2022-03-31 11:14:32
Query lines with the same number as the purchase quantity .
Number title Price Purchase quantity Refund status Order creation time Order payment time
0 1 T T-shirt 29.9 1 No refund requested 2022-03-31 21:54:46 2022-03-31 21:54:54
Check the price * The purchase quantity is greater than 1000 The line of .
Number title Price Purchase quantity Refund status Order creation time Order payment time
7 8 longuette 999.0 5 Refunded 2022-03-31 11:13:04 2022-03-31 11:14:32
9 10 T T-shirt 999.0 2 No refund requested 2022-03-29 23:22:42 2022-03-29 23:22:46
Multiple criteria query & and and
Number title Price Purchase quantity Refund status Order creation time Order payment time
1 2 T T-shirt 39.9 1 No refund requested 2022-03-31 21:54:46 2022-03-31 21:54:54
3 4 T T-shirt 39.9 1 No refund requested 2022-03-31 20:39:47 2022-03-31 20:39:53
13 14 shoes 39.9 1 No refund requested 2022-03-29 11:06:52 2022-03-29 11:06:57
Passing in variables @
Number title Price Purchase quantity Refund status Order creation time Order payment time
2 3 longuette 999.0 1 Refunded 2022-03-31 21:54:46 2022-03-31 21:54:54
5 6 T T-shirt 999.0 1 Refunded 2022-03-31 16:20:27 2022-03-31 16:20:33
7 8 longuette 999.0 5 Refunded 2022-03-31 11:13:04 2022-03-31 11:14:32
9 10 T T-shirt 999.0 2 No refund requested 2022-03-29 23:22:42 2022-03-29 23:22:46
12 13 shoes 999.0 1 Refunded 2022-03-29 13:28:01 2022-03-29 13:28:13
Process ended , Exit code 0

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved