← Guides

Clean a Messy Excel File in 15 Lines of Python with pandas

Stray spaces, duplicates, and wrong date formats: the minimal script to fix a customer export.

Python · 8 min read

By the FormulAI editorial team · Reviewed October 2026

Loading Without Type Errors

pd.read_excel(file, dtype=str) loads everything as text and prevents pandas from transforming a zip code like 01000 into 1000 or an order number into scientific notation. You can then explicitly convert the necessary columns.

Normalizing Text Columns

df['city'] = df['city'].str.strip().str.title() removes leading and trailing spaces and harmonizes casing. This is what reduces the number of "distinct" cities from 400 to 120 in a manually entered file.

Deleting Duplicates Intelligently

drop_duplicates(subset=['email'], keep='last') keeps the last entry, which is usually the most up-to-date. Without the subset parameter, pandas compares all columns and lets partial duplicates slip through.

Exporting Cleanly

df.to_excel('clean.xlsx', index=False) avoids the extra index column that everyone ends up deleting manually in Excel.

How this guide was made

Every formula, query and script in this guide was run on a real sample file (Excel 365, Google Sheets, PostgreSQL, MySQL or Python 3) before publication. We document the edge cases we hit during testing, such as text-formatted numbers, empty months or NULL values. AI helps us draft and translate; a human checks each example and updates the guide when software versions change. Spotted an error? Write to contact@formulai-app.com.