pandas Cheatsheet

Aggregation

Use this pandas reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Built-in Aggregation Methods

These work on both Series and DataFrame. DataFrame applies them column-wise by default (axis=0).

Numeric Aggregations

MethodDescription
df.sum()Sum (skips NaN by default)
df.mean()Arithmetic mean
df.median()50th percentile
df.min() / df.max()Minimum / maximum
df.std() / df.var()Sample std / variance (ddof=1 default)
df.sem()Standard error of the mean
df.prod()Product of all values
df.count()Non-null count
df.nunique()Number of distinct values
df.cumsum()Cumulative sum
df.cumprod()Cumulative product
df.cummax() / df.cummin()Cumulative max / min
df.abs()Absolute values (element-wise)
df.round(2)Round to N decimals
df.clip(0, 100)Clip values to [low, high]
df.quantile(0.25)Quantile
df.corr()Correlation matrix
df.cov()Covariance matrix
df.skew()Skewness
df.kurt()Excess kurtosis
df["score"].sum()
df["score"].mean()
df.mean(numeric_only=True)     # skip non-numeric columns
df.sum(axis=1)                 # row-wise sum
df.max(skipna=True)            # NaN are excluded (default)

Boolean Aggregations

df.any()           # True if any value is truthy per column
df.all()           # True if all values are truthy per column
df.any(axis=1)     # row-wise
(df > 0).all()     # element-wise condition then aggregate

.agg() / .aggregate() — Multi-function Aggregation

# Single function (string or callable)
df["score"].agg("mean")
df["score"].agg(np.sum)

# List of functions → Series (one value per function)
df["score"].agg(["mean", "std", "min", "max"])

# Dict of column → function(s)
df.agg({"score": "mean", "age": ["min", "max"]})

# Named aggregations on GroupBy
df.groupby("dept").agg(
    avg_salary=("salary", "mean"),
    max_age=("age", "max"),
    n=("id", "count"),
)

# Custom function
df["score"].agg(lambda s: s.quantile(0.9))

describe() — Summary Statistics

df.describe()                        # count, mean, std, min, 25%, 50%, 75%, max
df.describe(include="all")           # include object/category cols
df.describe(include=["object"])      # non-numeric only
df.describe(percentiles=[0.1, 0.9])  # custom percentiles
df["score"].describe()               # on a Series

value_counts() — Frequency Table

s.value_counts()                     # sorted by frequency
s.value_counts(normalize=True)       # relative frequencies (proportions)
s.value_counts(ascending=True)       # lowest first
s.value_counts(dropna=False)         # include NaN
s.value_counts(bins=5)               # bucket numeric into N bins

quantile() — Percentiles

df["score"].quantile(0.5)           # median
df["score"].quantile([0.25, 0.75])  # multiple percentiles → Series
df.quantile(0.9)                    # 90th percentile for each column
df.quantile([0.25, 0.5, 0.75])     # DataFrame of percentiles

idxmin() / idxmax() — Index of Extremum

df["score"].idxmax()       # index label of the max value
df["score"].idxmin()       # index label of the min value
df.idxmax()                # column-wise → Series of index labels
df.idxmax(axis=1)          # row-wise → column name with max per row

nlargest() / nsmallest()

df["score"].nlargest(5)                # top 5 values (Series)
df.nlargest(5, "score")               # top 5 rows by score
df.nlargest(5, ["score", "age"])      # tiebreak by age
df.nsmallest(10, "price")

Weighted and Rolling Aggregation

# Weighted average
np.average(df["score"], weights=df["weight"])

# Rolling window
df["val"].rolling(window=7).mean()          # 7-period moving average
df["val"].rolling(window=7, min_periods=1).mean()  # don't drop edge values
df["val"].rolling("7D").sum()               # time-based window (DatetimeIndex)
df["val"].rolling(7).agg(["mean", "std"])

# Expanding window (cumulative up to each point)
df["val"].expanding().mean()
df["val"].expanding(min_periods=3).sum()

Exponentially Weighted Moving Average (EWMA)

df["val"].ewm(span=10).mean()
df["val"].ewm(halflife=5).mean()
df["val"].ewm(alpha=0.3).mean()      # smoothing factor (0 < alpha <= 1)
df["val"].ewm(com=0.5).std()

Correlation and Covariance

df.corr()                            # Pearson correlation matrix (all numeric cols)
df.corr(method="spearman")           # "pearson" | "kendall" | "spearman"
df["a"].corr(df["b"])                # scalar correlation
df.corr(min_periods=30)              # require N observations

df.cov()                             # covariance matrix
df["a"].cov(df["b"])                 # scalar covariance

eval() — Fast Computed Aggregation

# Evaluate expression string (uses numexpr under the hood for large frames)
df.eval("score + bonus")                     # returns Series
df.eval("total = score + bonus", inplace=True)  # adds column in-place
result = df.eval("score.mean()")

pipe() — Chain Custom Aggregations

def add_total(df):
    df["total"] = df["price"] * df["qty"]
    return df

result = (
    df
    .pipe(add_total)
    .groupby("category")
    .agg(revenue=("total", "sum"), units=("qty", "sum"))
)