Pandas
Missing Data
Detect, fill, and drop NaN values.
By EZ4Code Team
missing-datanan
Code
import pandas as pd
import numpy as np
df = pd.DataFrame({
"a": [1, np.nan, 3, np.nan],
"b": [10, 20, np.nan, 40],
})
# Detect
print(df.isna(), df.isna().sum())
# Fill
filled = df.fillna({"a": 0, "b": df["b"].mean()})
ffill = df.ffill()
interp = df.interpolate()
# Drop
dropped_rows = df.dropna()
dropped_cols = df.dropna(axis=1)
clean = df.dropna(thresh=1)
print(filled, dropped_rows)Explanation
isna() identifies missing values and sum() per column gives a quick null-count overview. fillna replaces NaN with constants, per-column mappings, or methods like ffill and interpolate. dropna removes rows or columns with too many missing values, configurable with thresh.
More Pandas Snippets
DataFrame Creation
Build DataFrames from dicts, lists, and files.
Indexing and Selecting
Select rows and columns with loc, iloc, and masks.
GroupBy Operations
Split, aggregate, and transform with groupby.
Merge and Join
Combine frames with merge and concat.
Pivot Tables
Reshape data with pivot, melt, and pivot_table.
Time Series
Resample, shift, and roll windows over time data.