Python Pandas - 查询 DataFrame 的列
要查询 Pandas DataFrame 的列,请使用query(). 我们正在查询以过滤记录。首先,让我们创建一个 Pandas DataFrame
dataFrame = pd.DataFrame({"Product": ["SmartTV", "PenDrive", "Speaker", "Earphone"],"Opening_Stock": [300, 700, 1200, 1500],"Closing_Stock": [200, 500, 1000, 900]})
使用query()与条件查询列-
print(dataFrame.query('Opening_Stock >=500 & Closing_Stock < 1000 & Product.str.startswith("P").values'))
示例
以下是完整的代码 -
import pandas as pd输出结果dataFrame = pd.DataFrame({"Product": ["SmartTV", "PenDrive", "Speaker", "Earphone"],"Opening_Stock": [300, 700, 1200, 1500],"Closing_Stock": [200, 500, 1000, 900]})
print"DataFrame...\n",dataFrame
# using query() to query columns
print"\nQuerying columns to filter records...\n"
print(dataFrame.query('Opening_Stock >=500 & Closing_Stock < 1000 & Product.str.startswith("P").values'))
这将产生以下输出 -
DataFrame...Closing_Stock Opening_Stock Product
0 200 300 SmartTV
1 500 700 PenDrive
2 1000 1200 Speaker
3 900 1500 Earphone
Querying columns to filter records...
Closing_Stock Opening_Stock Product
1 500 700 PenDrive
以上是 Python Pandas - 查询 DataFrame 的列 的全部内容, 来源链接: utcz.com/z/349108.html