用多个条件过滤 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