Understanding the axis Argument in Pandas – axis=0 vs axis=1 Explained 2026
The axis parameter is one of the most important and frequently misunderstood concepts in Pandas. Mastering axis=0 (rows) versus axis=1 (columns) is essential for effective data manipulation.
TL;DR — axis Explained Simply
axis=0→ Operate **down the rows** (column-wise operation)axis=1→ Operate **across the columns** (row-wise operation)- Think: "0 = vertical (like the y-axis in math), 1 = horizontal"
1. Basic Examples of axis
import pandas as pd
df = pd.read_csv("sales_data.csv")
# axis=0 → along rows (default for most operations)
column_sums = df.sum(axis=0) # Sum of each column
column_means = df.mean(axis=0)
# axis=1 → along columns
row_sums = df.sum(axis=1) # Sum of each row
row_means = df.mean(axis=1)
2. Common Operations with axis
# Drop rows with missing values
df_clean = df.dropna(axis=0) # Drop rows (default)
# Drop columns with missing values
df_clean = df.dropna(axis=1) # Drop columns
# Mean of each column
col_means = df.mean(axis=0)
# Mean of each row
row_means = df.mean(axis=1)
# Fill missing values column-wise
df.fillna(df.mean(axis=0), axis=0) # Fill with column mean
3. Real-World Example with groupby + axis
# Sum sales by region (group along rows)
sales_by_region = df.groupby("region")["amount"].sum()
# Calculate percentage of total for each row
df["percentage"] = df["amount"] / df["amount"].sum(axis=0) * 100
# Normalize each row (row-wise operation)
df_normalized = df[["amount", "quantity"]].div(
df[["amount", "quantity"]].sum(axis=1),
axis=0
)
4. Best Practices in 2026
- Use
axis=0when you want to operate **per column** (most common) - Use
axis=1when you want to operate **per row** - Always be explicit with
axisinstead of relying on default behavior - When in doubt, remember:
axis=0affects rows (vertical),axis=1affects columns (horizontal) - Use meaningful variable names to avoid confusion when using
axis=1
Conclusion
The axis argument is fundamental to understanding how Pandas operations work. In 2026, always be explicit with axis=0 (operate down columns) and axis=1 (operate across rows). Mastering this concept will eliminate many common bugs and make your data manipulation code much more predictable and professional.
Next steps:
- Review your current Pandas code and make sure you are using
axisexplicitly and correctly in allsum(),mean(),dropna(), andfillna()operations