Data agentsIntermediate

Natural Language to Reproducible Pandas Code

A data agent becomes reviewable when a plain-language request leaves behind executable code, explicit assumptions, output data, and validation evidence.

PandasData cleaningAgentsReproducibility
Documents and retrieval paths converging on a luminous evidence core with connected agent nodes
Generated visual worldAI & knowledge systems

Evidence, retrieval, agents, and the paths that connect a question to a grounded answer.

Interactive model

From request to transformations

Toggle common transformations to see how raw columns become model-ready features or cleaned fields.

Live HTML simulation · adjust the controls and watch the computed output respond.

Interactive

Raw columns become model-ready signals through feature engineering

Raw inputsduration, miles, receipts
Generated featuresdurationmilesreceiptsmiles/dayreceipts/daylog(receipts+1)miles x receipts
Modeltree ensemble or regression

This is a simplified teaching model. Its displayed values are computed from the controls; the article explains where the model stops.

Site connection

The Data Cleaning Assistant reports a workflow that turns plain-English CSV cleaning requests into cleaned outputs and reusable pandas scripts.

Translation Is a Schema-and-Policy Problem

A request such as 'make hired a boolean and remove bad rows' leaves key questions unresolved: which strings count as true, what makes a row bad, how missing values should behave, whether row identity must be preserved, and which output is authoritative. Reproducible code must make those decisions visible.

A reviewable run should produce:

  1. A script with explicit inputs, transformations, and outputs.
  2. A cleaned dataset or other deterministic artifact.
  3. An audit summary with assumptions, before/after metrics, and validation results.

Reported Project Behavior and Recommended Contract

Project fact: the portfolio source says the Data Cleaning Assistant accepts a CSV and plain-English instructions, then returns a cleaned dataset plus the exact Python script. It reports a FastAPI and Google Gemini multi-agent backend, schema exploration, pandas/numpy code execution in a sandboxed environment with no `exec` or `eval` and whitelisted imports, visualization, statistical analysis, merging, regex, planning, and specialist routing.

The source demonstrates 'make the hired column a boolean' and reports that the agent ran `df['Hired'].astype(bool)` while the DataFrame shape stayed unchanged. That demonstration is not a generally correct parsing rule: in pandas/Python, a non-empty string such as `'False'` is truthy. The schema-first translation and validation policies below are recommendations for making such transformations dependable.

Reference table for this concept
StatementClassification
The project returns cleaned data and a Python scriptReported project behavior
The portfolio reports whitelisted imports and no exec/evalReported project guardrail
Boolean conversion should use an explicit value mapRecommended correctness policy
Every run should pin pandas and record input/output hashesRecommended reproducibility policy

Compile Intent into an Explicit Transformation Plan

Before generating code, inspect column names, dtypes, representative values, null counts, uniqueness, and row identifiers. Translate the request into operations with preconditions and postconditions. If a consequential rule remains ambiguous, present the assumption or ask for clarification rather than guessing from a column name.

Separate cleaning from analysis. Cleaning standardizes representation, repairs or flags invalid values, and preserves lineage. Aggregation, modeling, and plotting may consume the cleaned data, but they should remain distinct steps so a user can rerun or reject one without silently changing another.

Analogy limit: generated code resembles a compiler output because it translates a higher-level request. Natural language lacks a formal grammar and complete semantics, so schema inspection, confirmation, and tests remain necessary.
Reference table for this concept
Natural-language intentRequired decisionPostcondition
Make hired a booleanAccepted true/false tokens and null policyOnly True, False, or allowed NA remains
Remove bad rowsFormal invalidity predicateEvery removed row has a reason code
Deduplicate customersIdentity keys and keep ruleAt most one row per selected key
Fill missing incomeImputation method and groupingNo target NA; imputation flags retained
Graph followers vs followingNumeric coercion and exclusion rulesPlot inputs and excluded rows are reported

Reproducibility and Execution Safety

A reproducible script declares the input path or object, expected schema, dependency versions, ordered transformations, deterministic parameters, validation assertions, and output path. Record input and output hashes plus before/after row counts, dtypes, and null counts. Avoid hidden notebook state and broad `inplace` mutation; pandas 3.0's official Copy-on-Write behavior also means older assumptions about views, chained assignment, and side effects should not be carried across versions without testing.

Execution safety is a separate boundary from reproducibility. Recommended controls include a disposable working directory, read-only original input, narrow output directory, import allowlist, disabled network, resource and time limits, subprocess restrictions, file-size caps, and structured result capture. A ban on `exec`/`eval` and an import allowlist reduce risk but do not by themselves prove complete sandbox isolation.

Reference table for this concept
RiskGuardrailEvidence to retain
Wrong column assumptionSchema preview and explicit preconditionObserved columns and selected mapping
Silent row lossReason-coded filter and count assertionBefore/after counts by reason
Version-dependent behaviorPinned environment and fixture testspandas version and test output
Filesystem escapeIsolated read/write mountsDeclared accessed paths
Non-reproducible outputExact script, parameters, and hashesRun manifest

Worked Example

Input values in `Hired` are `['Yes', 'no', 'TRUE', '0', '', null]`. The user asks, 'make Hired a boolean and remove rows where the answer is blank.' An explicit policy maps `yes/true/1` to `True`, `no/false/0` to `False`, treats an empty string and null as missing, and removes only missing outcomes. The agent reports the policy before execution.

The generated script normalizes text with `str.strip().str.lower()`, maps through a fixed dictionary, stores the result using pandas' nullable `boolean` dtype, asserts that every non-missing normalized token was recognized, records two removed row IDs, and writes a new output instead of overwriting the upload. For the six illustrative rows, four remain: `[True, False, True, False]`.

Using `.astype(bool)` directly would be wrong for this string column because non-empty strings—including `'no'` and `'0'`—evaluate truthy. If an unexpected token such as `'maybe'` appears, the correct result is a failed validation or explicit unresolved category, not silent coercion.

Reference table for this concept
Raw valueNormalizedOutputReason
YesyesTrueExplicit true token
nonoFalseExplicit false token
TRUEtrueTrueCase normalized
00FalseExplicit false token
empty stringemptyremovedDeclared missing-row policy
nullNAremovedDeclared missing-row policy

Validation, Audit Output, and Limitations

Validate both structure and meaning: required columns, dtype expectations, allowed domains, key uniqueness, row-count bounds, missingness, distribution shifts, and invariants specific to the task. For grouping or imputation, remember that pandas GroupBy follows split-apply-combine and that count-like operations can differ in their handling of missing values; tests should encode the intended statistic rather than rely on a vague word such as 'count.'

Natural language cannot recover business rules that are absent from the request and data. A passing script can implement the wrong policy, a cleaned dataset can erase meaningful outliers, and deterministic code can reproduce a biased transformation perfectly. Preserve the original, expose changed cells or reason-coded row removals, and keep human review proportional to consequence.

Common Pitfalls

  • Converting string booleans with `astype(bool)` and turning every non-empty string into True.
  • Dropping rows under a vague 'bad row' rule without reason codes or before/after counts.
  • Returning only a cleaned CSV without the exact script, environment, and assumptions.
  • Assuming an import allowlist or a ban on exec/eval proves complete sandbox isolation.
  • Using chained assignment or version-sensitive mutation without pinned pandas tests.
  • Conflating cleaning with modeling and silently changing the analysis target.
  • Presenting recommended validation and sandbox policies as reported project features.

Sources and Further Reading

Related Explainers