Calculating Summary Statistics Across Columns in Pandas – axis=1 Best Practices 2026
When you need to calculate statistics **across columns** (horizontally, row by row), you must use axis=1. This is very different from the default axis=0 (which works down columns). In 2026, knowing when and how to use axis=1 is essential for tasks like calculating row totals, averages, or custom scores.
TL;DR — When to Use axis=1
- Use
axis=1when you want to operate **across columns** (per row) - Use
axis=0(default) when you want to operate **down columns** (per column)
1. Basic Summary Statistics Across Columns
import pandas as pd
df = pd.read_csv("sales_data.csv")
# Sum across columns (row totals)
df["total_per_row"] = df[["amount", "quantity", "discount"]].sum(axis=1)
# Mean across columns
df["average_per_row"] = df[["amount", "quantity", "profit"]].mean(axis=1)
# Maximum value in each row
df["max_value"] = df[["amount", "quantity", "profit"]].max(axis=1)
2. Real-World Examples
# Calculate total order value per row
df["order_total"] = df[["product_price", "shipping_cost", "tax"]].sum(axis=1)
# Calculate average of multiple score columns
df["overall_score"] = df[["exam1", "exam2", "exam3", "project"]].mean(axis=1)
# Find which metric is highest in each row
df["best_metric"] = df[["sales", "profit", "margin"]].idxmax(axis=1)
3. Advanced: Custom Calculations Across Columns
# Weighted average across columns
weights = [0.5, 0.3, 0.2]
df["weighted_score"] = (
df[["score1", "score2", "score3"]] * weights
).sum(axis=1)
# Percentage of total per row
cols = ["north_sales", "south_sales", "east_sales"]
df["north_percentage"] = df[cols].apply(lambda row: row["north_sales"] / row.sum() * 100, axis=1)
4. Best Practices in 2026
- Use
axis=1explicitly when calculating row-wise statistics - Select only the relevant numeric columns before applying
axis=1operations - Be careful with missing values — they can affect row-wise calculations (consider
.fillna(0)first if appropriate) - Use
.sum(axis=1),.mean(axis=1),.max(axis=1), etc. - For very complex row-wise logic, consider using
.apply(axis=1)with a custom function
Conclusion
Calculating summary statistics **across columns** (row by row) requires axis=1. In 2026, this is the standard way to compute totals, averages, weighted scores, or any custom row-level metric. Understanding the difference between axis=0 (default, column-wise) and axis=1 (row-wise) is fundamental to writing correct and efficient Pandas code.
Next steps:
- Identify places in your code where you calculate totals or averages across several columns and make sure you are using
axis=1