Skip to main content
Hello and welcome back. In this lesson we cover practical, efficient techniques for transforming small-to-medium datasets. These approaches are ideal for tasks such as cleaning sales records for a mid-sized retailer, preparing survey results for analysis, or building preprocessing steps inside an ETL pipeline. We’ll focus on clear, repeatable steps you can apply in production MLOps workflows using pandas or Polars.
A slide titled "Data Transformation" with a funnel graphic on the left where folder icons enter the top and a database icon comes out the bottom. The design uses a dark blue background with teal outlines and a small "© Copyright KodeKloud" in the corner.
Two Python libraries are especially useful for these dataset sizes: pandas and Polars. Each has strengths:
  • pandas: ubiquitous, feature-rich, and integrates with the broader PyData ecosystem (plotting, I/O, modeling).
  • Polars: designed for speed and parallelism, often achieving faster queries and lower memory usage for equivalent operations.
A dark-blue infographic titled "Pandas vs Polaris" shows two circles — Pandas on the left ("known for flexibility and versatility") and Polaris on the right ("optimized for performance and efficiency") — with a "VS" icon between them. A small note below references "cleaning messy customer feedback data for insights" alongside a cleaning icon.
Pandas is great for breadth of functionality and ecosystem integration (plotting, modeling, I/O). Polars is worth considering when you need faster query performance or lower memory usage for the same operations.
How this fits into MLOps
  • These libraries are commonly used inside ETL and preprocessing stages. Many production pipelines operate on small-to-medium datasets that are best handled with pandas or Polars rather than distributed systems.
  • Use these tools for repeatable, testable transformations before model training, validation, or reporting.
A presentation slide titled "Data Cleaning With Pandas" that asks "How does this fill into MLOps?" and shows two outcomes: "ETL pipelines and data transformation" on the left and "Smaller datasets, easily transformed" on the right with directional arrows.
Common preprocessing sequence We recommend following a consistent sequence when preparing small-to-medium datasets. This helps keep pipelines predictable and easy to test:
  1. Handle missing values
  2. Remove duplicates
  3. Standardize data types
  4. Filter, sort, and perform row/column transformations
  5. Aggregate for insights
  6. Merge / join datasets
The rest of this document shows patterns and code examples (with pandas) for each step. The same conceptual steps apply in Polars — consult Polars docs for syntax differences. Key operation mappings (quick reference) Detailed patterns and examples

Missing values

Missing or malformed values can skew analyses and break downstream code. Prefer explicit assignment over using inplace=True to keep transformations clear and avoid deprecation surprises. Example patterns:
For numeric conversion, coerce invalid strings to NaN and then decide whether to drop or impute:
Avoid relying on inplace=True. Assigning the result back to a variable (df = df.dropna()) improves readability, reduces surprises, and is compatible with future pandas behavior.

Removing duplicates

Duplicate rows can distort counts and aggregates. Use drop_duplicates() and specify a subset of columns if you only want uniqueness on specific keys.

Standardizing data types

Consistent data types are crucial for correct math, joins, and model input. Common helpers are pd.to_numeric, pd.to_datetime, and astype.

Filtering and sorting

Use boolean indexing to filter rows and sort_values to order results.

Aggregation (groupby and agg)

Aggregate for reporting or feature engineering with groupby and agg. Reset the index after aggregation to keep a flat DataFrame.

Merging and joining datasets

Use pd.merge to combine tables — this is conceptually equivalent to SQL joins.

Consolidated example pipeline

The following concise example demonstrates a complete flow: cleaning → conversion → transformation → aggregation → join. This is a practical pattern you can adapt to ETL jobs and MLOps preprocessing steps.

Typical transformation flow (conceptual)

  • Raw data ingestion (CSV, Parquet, DB, API)
  • Missing-value handling (drop or impute per-column)
  • Data type standardization (numeric, datetime, categorical)
  • Data transformation (filter, compute features, normalize)
  • Aggregation (groupby + agg) for summaries or feature creation
  • Merge/join to combine related datasets
References and further reading
  • pandas documentation: https://pandas.pydata.org/
  • Polars documentation: https://www.pola.rs/
  • MLOps and ETL best practices: consider idempotent transformations, unit tests for preprocessing logic, and small, testable pipeline steps.
With these patterns, you have a compact, practical toolkit to clean and transform small-to-medium datasets using pandas or Polars. Apply these steps in your ETL pipelines and MLOps workflows to produce reliable, production-ready datasets.

Watch Video