用多個條件過濾 Pandas DataFrame
Suraj Joshi
2023年1月30日
2021年1月22日
本教程解釋瞭如何基於多個條件從 DataFrame 中過濾元素。
我們將在本文中使用下面的 DataFrame。
import pandas as pd
stocks_df = pd.DataFrame({
'Stock': ["Tesla","Moderna Inc","Facebook","Boeing"],
'Price': [835,112,267,209],
'Sector':["Technology","Health Technology","Technology","Aircraft"]
})
print(stocks_df)
輸出:
Stock Price Sector
0 Tesla 835 Technology
1 Moderna Inc 112 Health Technology
2 Facebook 267 Technology
3 Boeing 209 Aircraft
根據多個條件使用索引過濾 DataFrame 的元素
import pandas as pd
stocks_df = pd.DataFrame({
'Stock': ["Tesla","Moderna Inc","Facebook","Boeing"],
'Price': [835,112,267,209],
'Sector':["Technology","Health Technology","Technology","Aircraft"]
})
print("Stocks DataFrame:")
print(stocks_df,"\n")
reqd_stocks = stocks_df[(stocks_df.Sector == "Technology") & (stocks_df.Price <500)]
print("The stocks of technology sector with price less than 500 are:")
print(reqd_stocks)
輸出:
Stocks DataFrame:
Stock Price Sector
0 Tesla 835 Technology
1 Moderna Inc 112 Health Technology
2 Facebook 267 Technology
3 Boeing 209 Aircraft
The stocks of technology sector with price less than 500 are:
Stock Price Sector
2 Facebook 267 Technology
它過濾了 stocks_df
中的所有元素,其中 Sector
列的值是 Technology
,Price
列的值小於 500。
我們在 []
內指定條件,用&
或|
運算子連線條件,根據多個條件對數值進行索引。&
運算子代表邏輯和
,意思是這兩個條件必須為真才能選擇一個元素。|
運算子代表邏輯或
,意思是如果滿足任何條件就可以選擇一個元素。
使用 query()
方法基於多個條件過濾 DataFrame 的元素
我們將由&
或|
運算子連線的多個條件作為引數傳遞給 query()
方法。
import pandas as pd
stocks_df = pd.DataFrame({
'Stock': ["Tesla","Moderna Inc","Facebook","Boeing"],
'Price': [835,112,267,209],
'Sector':["Technology","Health Technology","Technology","Aircraft"]
})
print("Stocks DataFrame:")
print(stocks_df,"\n")
reqd_stocks = stocks_df.query("Sector == 'Technology' & Price <500")
print("The stocks of technology sector with price less than 500 are:")
print(reqd_stocks)
輸出:
Stocks DataFrame:
Stock Price Sector
0 Tesla 835 Technology
1 Moderna Inc 112 Health Technology
2 Facebook 267 Technology
3 Boeing 209 Aircraft
The stocks of technology sector with price less than 500 are:
Stock Price Sector
2 Facebook 267 Technology
Author: Suraj Joshi
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn