pythonbeginner
Pandas Null Handling Strategies
Comprehensive strategies for detecting, filling, and handling missing values in pandas DataFrames.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
import numpy as np
df = pd.read_csv("data.csv")
# Detect nulls
print("Null counts:")
print(df.isna().sum())
print(f"\nTotal null cells: {df.isna().sum().sum()}")
print(f"Rows with any null: {df.isna().any(axis=1).sum()}")
# Drop rows/columns with nulls
df_clean = df.dropna() # drop any row with nulls
df_thresh = df.dropna(thresh=3) # keep rows with >= 3 non-null values
df_subset = df.dropna(subset=["email", "name"]) # only check specific columns
# Fill with constants
df["category"] = df["category"].fillna("unknown")
df["score"] = df["score"].fillna(0)
# Fill with statistics
df["amount"] = df["amount"].fillna(df["amount"].median())
df["rating"] = df["rating"].fillna(df["rating"].mean())
# Forward/backward fill (time series)
df["price"] = df["price"].ffill() # carry forward last known value
df["price"] = df["price"].bfill() # fill backward from next known value
# Group-specific fill
df["salary"] = df.groupby("department")["salary"].transform(
lambda x: x.fillna(x.median())
)
# Interpolate numeric values
df["temperature"] = df["temperature"].interpolate(method="linear")
# Replace sentinel values with NaN
df = df.replace({-999: np.nan, "N/A": np.nan, "": np.nan})
print(f"\nAfter cleaning: {df.isna().sum().sum()} nulls remaining")Use Cases
- Cleaning datasets with missing values
- Preparing data for machine learning models
- Handling incomplete records in ETL pipelines
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonbeginner
Pandas String Operations
Clean, extract, and transform string columns using pandas .str accessor methods.
Best for: data cleaning
#pandas#strings
pythonbeginner
Detect & Remove Pandas Duplicates
Find, count, and remove duplicate rows with flexible keep strategy and composite key support.
Best for: data cleaning
#pandas#duplicates
pythonbeginner
Pandas DataFrame Transformations
Common pandas DataFrame transformations including column operations, type casting, and string methods.
Best for: Cleaning raw data files for analysis
#pandas#dataframe
pythonbeginner
Pandas DataFrame Filtering Techniques
Filter DataFrames using boolean masks, query syntax, isin, between, and string matching methods.
Best for: Extracting subsets of data for reporting
#pandas#filtering