bhylu.com

Advertisements

Pandas DataFrame Query

DataFrame.query offers a concise expression interface, but its expression text is code-like input rather than a safe general user-query language.

How it works

Use a fixed expression authored by the application and pass a validated value through a local variable, or prefer explicit Boolean operations. Do not accept arbitrary expression strings from an untrusted form. The Python engine is chosen explicitly here so the example does not silently depend on an optional numexpr installation. A bounded selection still needs a documented visibility and input policy.

Run the code

Select Try in playground to load the complete editable source. Nothing executes until you choose Run. The browser uses Pyodide 0.27.7, Pandas 2.2.3 and NumPy 2.0.2. Each ordinary execution starts a fresh isolated runtime. The tables are fictional and do not use personal files.

For local work, use a separate virtual environment and run python fixed-queries.py. Download the complete source. The reviewed local environment uses Python 3.12.14, Pandas 3.0.6 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with Pandas 2.2.3 and NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.

Example

fixed-queries.py

import pandas as pd
import numpy as np

df = pd.DataFrame({
    "drop_id": [101, 102, 103, 104],
    "point": ["north", "south", "north", "south"],
    "material": ["leaves", "food", "food", "leaves"],
    "kg": [12., 8., 6., 10.],
    "bags": pd.array([3, 2, 1, 2], dtype="Int64"),
    "day": ["2026-10-01", "2026-10-01", "2026-10-02", "2026-10-02"],
})

minimum = 10.
selected = df.query("kg >= @minimum", engine="python")
print(selected["drop_id"].tolist())
print(float(selected["kg"].sum()))

Expected result

[101, 104]
22.0

The threshold remains a value rather than being concatenated into query source. The expression itself is trusted application code and selects the same records as an explicit ge mask. This example is not a sandbox for arbitrary expressions.

Use Pandas’s official reference for precise contracts, supported versions and numerical limitations.