Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Use axis=0 and axis=1 in Pandas?
In Pandas, the axis parameter controls the direction of operations on DataFrames and Series. Understanding axis=0 and axis=1 is essential for performing row-wise and column-wise operations efficiently.
Understanding Axis in Pandas
A Pandas DataFrame is a two-dimensional table with rows and columns. The axis parameter determines how operations are applied:
Axis 0 (rows) ? Operations are applied along rows, working down vertically. Results are computed across rows for each column.
Axis 1 (columns) ? Operations are applied along columns, working across horizontally. Results are computed across columns for each row.
Using axis=0 (Row Operations)
When axis=0, operations are performed down the rows, producing results for each column.
Syntax
result = df.operation(axis=0)
Example
Let's sum values along axis=0 to get column totals:
import pandas as pd
import numpy as np
# Create sample DataFrame
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
columns=['A', 'B', 'C'])
print("DataFrame:")
print(df)
# Sum along axis=0 (down rows)
row_sum = df.sum(axis=0)
print("\nSum along axis=0 (column totals):")
print(row_sum)
DataFrame: A B C 0 1 2 3 1 4 5 6 2 7 8 9 Sum along axis=0 (column totals): A 12 B 15 C 18 dtype: int64
Using axis=1 (Column Operations)
When axis=1, operations are performed across columns, producing results for each row.
Syntax
result = df.operation(axis=1)
Example
Let's sum values along axis=1 to get row totals:
import pandas as pd
import numpy as np
# Create sample DataFrame
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
columns=['A', 'B', 'C'])
print("DataFrame:")
print(df)
# Sum along axis=1 (across columns)
column_sum = df.sum(axis=1)
print("\nSum along axis=1 (row totals):")
print(column_sum)
DataFrame: A B C 0 1 2 3 1 4 5 6 2 7 8 9 Sum along axis=1 (row totals): 0 6 1 15 2 24 dtype: int64
Combining Operations
You can chain operations with different axis values to perform complex calculations:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
columns=['A', 'B', 'C'])
# Find maximum value in each row (axis=1)
row_max = df.max(axis=1)
print("Maximum value in each row:")
print(row_max)
# Sum all maximum values (axis=0)
total_max = row_max.sum()
print("\nSum of maximum values:")
print(total_max)
Maximum value in each row: 0 3 1 6 2 9 dtype: int64 Sum of maximum values: 18
Common Operations with Axis
| Operation | axis=0 (Rows) | axis=1 (Columns) |
|---|---|---|
sum() |
Column totals | Row totals |
mean() |
Column averages | Row averages |
max() |
Column maximums | Row maximums |
drop() |
Drop rows | Drop columns |
Conclusion
Understanding axis=0 and axis=1 is crucial for DataFrame operations. Remember: axis=0 works down rows (column-wise results), while axis=1 works across columns (row-wise results). Practice with different operations to master this fundamental concept.
