Loading and Cleaning Data with Pandas
Learn to import data from common file formats, inspect its structure and quality, and systematically clean messy real-world datasets using pandas—handling missing values, fixing inconsistent formats, and preparing data for analysis.
Loading video…
What you'll be able to do
- Load data from CSV, Excel, and other formats into pandas DataFrames with appropriate parameters
- Inspect a dataset's structure, types, and quality using core pandas methods
- Detect and handle missing values using strategies like dropping and imputation
- Standardize inconsistent text, dates, and numeric formats
- Remove duplicates and apply type conversions to produce analysis-ready data
Why Data Cleaning Matters
Real-world data is messy: missing values, inconsistent capitalization, mixed date formats, stray whitespace, and duplicate rows are the norm, not the exception. Data scientists routinely spend the majority of a project on loading and cleaning data before any modeling begins. Pandas is the workhorse library for this work in Python.
Loading Data
Pandas can read many formats. The most common is CSV:
import pandas as pd
df = pd.read_csv('sales.csv')
Useful parameters:
sep— delimiter (e.g.';'or'\t')na_values— extra strings to treat as missing (e.g.['NA', '-', 'unknown'])parse_dates— columns to parse as datetimesdtype— force column typesusecols— load only needed columns
df = pd.read_csv('sales.csv', na_values=['-', 'n/a'], parse_dates=['order_date'])
Other readers: pd.read_excel(), pd.read_json(), pd.read_parquet(), pd.read_sql().
Inspecting the Data
Always look before you clean:
df.head() # first rows
df.shape # (rows, columns)
df.info() # dtypes and non-null counts
df.describe() # summary stats for numeric columns
df.isna().sum() # missing values per column
df['city'].value_counts() # category frequencies
info() and isna().sum() are your fastest signals of data-quality problems.
Handling Missing Values
First understand why values are missing, then choose a strategy:
- Drop:
df.dropna()removes rows;df.dropna(axis=1)removes columns. Usesubset=andthresh=for control. - Impute: fill with a sensible value.
df['age'] = df['age'].fillna(df['age'].median())
df['category'] = df['category'].fillna('unknown')
Use the median for skewed numerics, the mean for symmetric data, and the mode or a placeholder for categories. Avoid dropping data carelessly—you may lose valuable signal.
Fixing Inconsistent Formats
Text is a common offender:
df['city'] = df['city'].str.strip().str.lower()
df['city'] = df['city'].replace({'ny': 'new york', 'nyc': 'new york'})
Convert types explicitly:
df['price'] = pd.to_numeric(df['price'], errors='coerce')
df['order_date'] = pd.to_datetime(df['order_date'], errors='coerce')
Using errors='coerce' turns unparseable values into NaN/NaT so you can handle them deliberately.
Removing Duplicates
df.duplicated().sum() # how many duplicate rows
df = df.drop_duplicates() # remove exact duplicates
df = df.drop_duplicates(subset=['customer_id'], keep='last')
A Practical Cleaning Workflow
- Load with the right parameters.
- Inspect shape, types, and missingness.
- Standardize column names:
df.columns = df.columns.str.strip().str.lower().str.replace(' ', '_'). - Fix data types.
- Handle missing values.
- Standardize text/categories.
- Remove duplicates.
- Validate the result with another
info()/describe().
Keep cleaning steps in a reproducible script or notebook so the same raw data always produces the same clean output.
Check your understanding
6 questions — answer to see instant feedback.
drop_duplicates() with no arguments removes rows that are exact duplicates across all columns, keeping the first occurrence by default.
Ask the AI tutor about this lessonStuck or curious? Ask a question and get a grounded answer.
The tutor answers from this lesson's material and can make mistakes — verify anything important.