- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Use axis=0 and axis=1 in Pandas?
As a programmer, when we need to deal with data, we may need to perform operations only in rows or columns, or both. In pandas, the axis refers to how a function or an operation is applied to the Data Frame or the series. Pandas only can take two values, either 0 or 1, as an argument to the axis property. In this article, we will learn how to use the axis=0 and axis=1 in Pandas.
Understand the axis
Before we move forward, let us brief you on the axis of pandas. As we know, the data frame in pandas is a two-dimensional table constant of rows and columns.
Axis 0 − Axis 0 refers to the rows of a data frame or a Series. It is often referred to as the "index axis". When an operation is applied along axis 0, it means that the operation is applied to each row of the Data Frame or Series.
Axis 1 − Axis 1 refers to the columns of a data frame or a Series. It is often referred to as the "column axis". When an operation is applied along axis 1, it means that the operation is applied to each column of the Data Frame or Series.
Using axis=0
As mentioned earlier, axis=0 refers to the rows of a data frame or Series. We can apply any built-in function to the rows using this property. Note that this takes care of any null values if present in the data. It will simply ignore the element in such cases.
Syntax
row_operation = df.<operation>(axis=0)
Here df is the data frame we need to apply the operation. <operation> is the operation that we need to perform. For example, “sum” is a valid operation. Make sure the operation is valid. axis=0 specifies that we need to apply the operation row-wise.
Example
In the below code, we first imported the pandas and the numpy library with alias names. Next, we creed a data frame named df using the Data Frame method of the panda's library. We applied a summation operation to the data frame along axis=0, which means row-wise. Here the value of the first, second, and third rows is 1,4.7; hence we got the result 1+4+7 =12 for the first column
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']) row_sum = df.sum(axis=0) print(row_sum)
Output
A 12 B 15 C 18 dtype: int64
Using axis=1
axis=1 refers to the columns of a data frame or Series. We can apply any built-in function to the columns using this property. Note that this also takes care of any null values if present in the data. It will simply ignore the element in such cases.
Syntax
column_operation = df.<operation>(axis=1)
Here df is the data frame we need to apply the operation. <operation> is the operation that we need to perform. For example, “sum” is a valid operation. Make sure the operation is valid. axis=1 specifies that we need to apply the operation column-wise.
Example
In the below code, we first imported the pandas and the numpy library with alias names. Next, we creed a data frame named df using the Data Frame method of the panda's library. We applied a summation operation to the data frame along axis=1, which means column-wise. Here the value of the first, second, and third rows is 1,2.3; hence we got the result 1+2+3 =6 for the first column and so on.
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']) column_sum = df.sum(axis=1) print(column_sum)
Output
0 6 1 15 2 24 dtype: int64
Use Both The Operations
It is also possible to use both operations one by one. Bu this way, we can access any element and virtually traverse the entire data frame. For example, if we use axis=0 first, we can traverse all the rows, and by using axis=1, we can traverse the columns within the rows.
Example
We created a data frame named df in the following code using the Data Frame panda's method. Next, we used the max method and axis=1 to apply the max operation on the columns; after that, we used the sum method and axis=0 to apply the summation operations on the rows of each
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']) row_max = df.max(axis=1) print("Maximum value in each row:") print(row_max) total_max = row_max.sum(axis=0) print("Sum of maximum values:") print(total_max)
Output
Maximum value in each row: 0 3 1 6 2 9 dtype: int64 Sum of maximum values: 18
Conclusion
In this article, we learned how to use the axis=0 and axis=1 in Pandas. This concept is particularly useful when dealing with the rows or columns in the panda's data frame. We recommend the readers try the distribution on a few more datasets to have more confidence in the topic.