The axis argument is one of the most important (and sometimes confusing) concepts in pandas. It tells almost every method whether to operate along rows (axis=0 or 'index') or along columns (axis=1 or 'columns'). Get this right, and your summaries, drops, concatenations, and groupbys will behave exactly as expected.
In 2026, understanding `axis` is still essential — even as Polars and other libraries gain popularity. Here’s a clear, practical guide with real examples and the most common gotchas.
1. Quick Reference: What axis Means
axis=0or'index'? operate down rows (collapse rows, keep columns)axis=1or'columns'? operate across columns (collapse columns, keep rows)
2. Common Methods Where axis Matters
Summarization (mean, sum, max, etc.)
import pandas as pd
df = pd.DataFrame({
'Sales': [100, 200, 150],
'Profit': [20, 50, 30],
'Quantity': [10, 15, 12]
})
# Sum per column (axis=0, default)
print(df.sum(axis=0)) # or df.sum()
# Sum per row (axis=1)
print(df.sum(axis=1))
Output:
Sales 450
Profit 100
Quantity 37
dtype: int64
0 130
1 265
2 192
dtype: int64
Dropping rows or columns
# Drop column 'Quantity'
df_dropped_col = df.drop('Quantity', axis=1)
# Drop row 0
df_dropped_row = df.drop(0, axis=0)
Concatenation
df2 = pd.DataFrame({'Sales': [300, 400], 'Profit': [60, 80]})
# Stack rows (axis=0, default)
concat_rows = pd.concat([df, df2], axis=0)
# Stack columns (axis=1)
concat_cols = pd.concat([df, df2], axis=1)
3. Real-World Examples (2026 Use Cases)
Row-wise calculations (e.g., total per order)
df['Total'] = df[['Sales', 'Profit']].sum(axis=1)
print(df['Total'])
# 0 120
# 1 250
# 2 180
Column-wise normalization
# Normalize each column to z-score (axis=0)
df_normalized = (df - df.mean(axis=0)) / df.std(axis=0)
Dropping empty rows/columns
# Drop columns that are all NaN
df_clean = df.dropna(axis=1, how='all')
# Drop rows with any NaN
df_clean_rows = df.dropna(axis=0)
4. Modern Alternative in 2026: Polars
For large datasets, Polars is often faster and more memory-efficient — axis is replaced by explicit direction.
import polars as pl
df_pl = pl.DataFrame(data)
# Sum across columns (axis=1 equivalent)
row_sums_pl = df_pl.sum_horizontal()
# Sum down rows (axis=0 equivalent)
col_sums_pl = df_pl.sum()
Best Practices & Common Pitfalls
- Always specify axis explicitly — defaults vary (e.g., sum defaults to axis=0, but many forget)
- Remember: axis=0 collapses rows (vertical), axis=1 collapses columns (horizontal)
- Check shapes after operations —
axis=0keeps columns,axis=1keeps rows - Prefer string names ('index'/'columns') over 0/1 — more readable and future-proof
- For huge data, prefer Polars — explicit direction avoids axis confusion
Conclusion
The axis argument is pandas' way of saying "do this vertically (axis=0) or horizontally (axis=1)." Master it, and you'll control summarization, dropping, concatenation, normalization, and more with precision. In 2026, always be explicit with axis, prefer string labels for clarity, and reach for Polars when performance matters. Get axis right, and your pandas code becomes predictable, readable, and powerful.
Next time you sum, drop, or concat — think "axis=0 for rows, axis=1 for columns" — and you'll never mix them up again.