Understanding Axis In Pandas

Basic Concepts

  • axis = 0: Refers to rows, Operations are performed on each column.
  • axis = 1: Refers to columns, Operations are performed on each row.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
})

sum_0 = df.sum(axis=0) # axis = 0, means rows, Column-wise sum
print(sum_0)
"""
A 6
B 15
C 24
dtype: int64
"""

sum_1 = df.sum(axis=1) # axis = 1, means columns, Row-wise sum
print(sum_1)
"""
0 12
1 15
2 18
dtype: int64
"""

Column-wise
Row-wise
“-wise” 是一个常见的英语后缀,加在名词后构成副词,表示“就……而言”或“按照……的方式”


Understanding Axis In Pandas
https://jackiedai.github.io/2025/11/28/011Python/011Understand_Axis_In_Pandas/
Author
Lenthiu
Posted on
November 28, 2025
Licensed under