Analysis

Convert And Encode Dataset Columns

Handles the type-conversion and encoding grunt work needed before a dataset is ready for analysis or modeling. - Input: a list of columns with their current and target types - Converts types with edge-case handling (stray text in numeric columns, mixed-type columns) - Encodes categoricals appropriately: ordinal, one-hot/target, or frequency encoding for high-cardinality fields - Output: Python code plus a before/after conversion report flagging failed conversions

Prompt
My dataset has columns needing type conversion and encoding before analysis:

Current state:
{column_conversions}

Write Python code that:
1. Converts each column to the target type, with error handling for edge cases (what happens if "N/A" shows up in a numeric column?)
2. Encodes categorical variables appropriately:
   - Ordinal categories (low/medium/high) → ordinal encoding
   - Nominal categories (red/blue/green) → one-hot or target encoding
   - High-cardinality categories (1000+ values) → frequency or target encoding
3. Deals with mixed-type columns (both numbers and strings present)
4. Builds a conversion report: original dtype, new dtype, failed conversions, and before/after sample values
Download .md

Variables