# A Cleaning Routine

A Cleaning Routine: A Cleaning Function, Step by Step, Saving the Clean Data: to_csv and to_excel.

Updated: November 19, 2026

1. [A Cleaning Function, Step by Step](https://www.ask-codey.com/lessons/pandas-cleaning-function) (Journeyman): Cleaning steps scattered across notebook cells are hard to run again the same way, so each step becomes a function that starts with result = df.copy() and returns a new table, one clean(raw) function runs them all in order, method chaining with pipe, assign and rename writes it as one readable recipe, and in pandas 3 a single value is changed with .loc.
2. [Saving the Clean Data: to_csv and to_excel](https://www.ask-codey.com/lessons/pandas-saving-data) (Apprentice): A clean table lives only as long as the notebook's kernel, so to_csv with index=False saves it as a file the next notebook can load, to_excel saves a workbook for people who use Excel, and keeping data/raw and data/clean folders, with a save function that refuses the raw folder, means the raw file is never written over.

Source: https://www.ask-codey.com/courses/python-data-analysis/topics/data-a-cleaning-routine
