Clean CSV Data With Pandas: 10 Useful Commands
Ten pandas commands that fix most messy CSV files: spaces, capitals, duplicates, wrong types and missing values, shown in one example you can run.
Short answer: to clean CSV data with pandas, load it into a DataFrame with pd.read_csv(), inspect it with info() and isna().sum(), then fix text, types, duplicates and missing values step by step. Ten commands cover most real-world cleaning, and each one is shown below in a single example you can run.
What Is a DataFrame and Why Clean Data First?
A pandas DataFrame is a table with rows and named columns, like a spreadsheet inside Python. Real CSV files are messy: extra spaces, inconsistent capitals, repeated rows, blanks and numbers stored as text. Models and charts built on messy data give wrong answers, so cleaning comes first. If DataFrames are new to you, start with pandas DataFrames for AI.
The 10 Commands
# | Command | What it does |
|---|---|---|
1 |
| Loads the CSV into a DataFrame |
2 |
| Shows columns, types and non-empty counts |
3 |
| Counts missing values per column |
4 |
| Tidies column names |
5 |
| Removes spaces and standardises capitals |
6 |
| Removes repeated rows |
7 |
| Converts text to numbers; bad values become NaN |
8 |
| Converts text to dates; bad values become NaT |
9 |
| Fills missing values |
10 |
| Drops rows missing an essential field |
A Complete Example
Install pandas with pip install pandas. The data below is messy on purpose: spaces in the headers and names, different capitals, a duplicate row, a letter where an age should be, an invalid date and a missing score.
import pandas as pd
from io import StringIO
raw = StringIO("""Name , City,Age,Joined,Score
asha ,North Bay,29,2024-01-05,81
Ravi,north bay,,2024-02-10,74
asha ,North Bay,29,2024-01-05,81
Meera,Hill Town,abc,not a date,90
Tom,Hill Town,41,2024-03-02,
""")
df = pd.read_csv(raw) # 1. load
df.info() # 2. inspect types and gaps
print(df.isna().sum()) # 3. count missing values
df.columns = df.columns.str.strip().str.lower() # 4. tidy column names
df["name"] = df["name"].str.strip().str.title() # 5. fix text
df["city"] = df["city"].str.strip().str.title()
df = df.drop_duplicates() # 6. remove repeated rows
df["age"] = pd.to_numeric(df["age"], errors="coerce") # 7. text to numbers
df["joined"] = pd.to_datetime(df["joined"], errors="coerce") # 8. text to dates
df["age"] = df["age"].fillna(df["age"].median()) # 9. fill gaps
df = df.dropna(subset=["joined"]).reset_index(drop=True) # 10. drop unusable rows
print(df)
Because column names are tidied in step 4, the later steps use lower-case names. After cleaning, the two asha rows become one, Ravi's blank age is filled with the median age, and Meera's row is dropped because its date is invalid (its abc age had already become a missing value). The score column keeps its blank, because we did not decide how to treat it.
How Do You Decide What to Do With Missing Values?
Drop the row when the missing field is essential, such as a date you need for every record.
Fill with a typical value (median or most common) when the column matters but a few gaps are harmless. The median resists extreme values better than the mean.
Leave it blank on purpose when “unknown” is itself meaningful.
Write down what you chose, since the choice affects your results. For a deeper tour of this topic, see data cleaning with pandas.
Common Mistakes
Cleaning the original file. Keep the raw CSV untouched and save the cleaned result as a new file with
df.to_csv("clean.csv", index=False).Dropping duplicates before cleaning text. “ asha ” and “Asha” only match after you strip and standardise.
Silencing errors blindly.
errors="coerce"hides bad values as NaN, so count them afterwards and look at what was lost.Filling with a number without checking the type. Fill only after converting to numbers.
Key Takeaway
Inspect first, then fix names, text, types, duplicates and gaps, in that order. Keep the raw file, check what each step changed, and your pandas DataFrame will be ready for analysis or a model. You can read and write CSV and JSON in plain Python too, as shown in files, JSON and CSV.
Ready to start? Learn Python and pandas step by step, with exercises on real, messy data. Begin with the free course Python for AI: From Zero to Your First AI App.
Related articles
More in Python
Python Virtual Environments (venv) Explained
A Python venv keeps each project's packages separate so projects cannot break each other. Learn the exact commands for macOS, Linux and Windows, plus requirements.txt.
5 min readNov 29, 2026What Is scikit-learn? The First Machine Learning Library to Learn
scikit-learn is the standard Python library for classic machine learning. See what it does, how to install it, and a complete first model in a few lines.
4 min readNov 27, 2026Web Scraping With Python: A Beginner's Guide
Learn the basics of web scraping with Python using requests and BeautifulSoup, and the rules that keep you polite and safe: robots.txt, terms of service, rate limits and personal data.
4 min readNov 24, 2026TensorFlow vs PyTorch: Which Should Beginners Learn?
TensorFlow and PyTorch teach the same core ideas. Compare them for beginners, see what they automate with a small runnable Python example, and learn how to choose without wasting weeks.
4 min readNov 19, 2026Jupyter vs VS Code vs Google Colab
Where should you write your first Python code? Compare Jupyter Notebook, VS Code and Google Colab, with a simple rule for choosing.
4 min readNov 15, 2026Your First Python Project: Build a Calculator
Build a command-line calculator in about 30 lines of Python, learning functions, loops, conditions and error handling along the way.
3 min readNov 12, 2026
Comments
Sign in to join the discussion.

