. Advertisement .
. Advertisement .
Pandas should never be a strange name for coders, which is a powerful toolbox library. It contains two main data structures: DataFrame and Series and a crucial parameter: axis.
While it is a key factor for the efficient library operation, it is often poorly described and underrated by various coding beginners. This article will offer a deeper understanding of Pandas axis meaning and its uses.
What Is Axis In Pandas?
Axis or axes are identified for an array having more than one dimension. For example, with a dual-dimensional array, you will receive two axes: axis 0 and axis 1.
Specifically, the axis 0 runs downwards vertically across the rows. On the other hand, axis 1 runs across columns horizontally. Each axis shows different values.
Axis = 0
The axis=0 represents the column-wise function. So when the axis returns to zero, it means that your program is performing effectively and satisfies the condition.
Here is an example of this case:
# importing packages import pandas as pd # importing our dataset df = pd.read_csv('hiring.csv') # dropping the column named 'experience' df = df.drop([0, 3], axis=0) # 'viewing the dataframe df.head()
Axis = 1
On the other hand, the axis=1 parameter represents the row-wise DataFrame function. When you get the axis set to this value, it means that the program is satisfying the column condition.
# importing packages import pandas as pd # importing our dataset df = pd.read_csv('hiring.csv') # dropping the column named 'experience' df = df.drop(['experience'], axis=1) # 'viewing the dataframe df.head()
Some Uses Of Axis
Find The Mean Value
One of the very first uses of axis is to determine the mean value for both columns and rows. Use the axis=0 command to get the column’s mean value:
#find mean of each column df.mean(axis=0) points 20.250 assists 7.750 rebounds 8.375 dtype: float64
The output shows the numeric column value. In this case, it is worth noting that the program doesn’t calculate all the character columns automatically.
The axis=1 command is used to find the row’s mean value:
#find mean of each row df.mean(axis=1) 0 13.666667 1 9.000000 2 10.666667 3 9.666667 4 12.333333 5 12.333333 6 14.333333 7 15.000000 dtype: float64
From the output, you can easily get the mean value of each row.
Find The Sum Value
Like finding the mean value, each axis will work out the result for each corresponding line. In particular, the axis=0 gives the sum of some chosen DataFrame columns while the axis=1 finds that of chosen rows.
#find sum of each row df.sum(axis=1) 0 41 1 27 2 32 3 29 4 37 5 37 6 43 7 45 dtype: int64
Find The Max Value
Find the max value in specific columns with the following code:
#find max of 'points', 'assists', and 'rebounds' columns df[['points', 'assists', 'rebounds']].max(axis=0) points 29 assists 12 rebounds 12 dtype: int64
For rows, use the general command:
#find max of each row df.max(axis=1)
The article discusses what Pandas axis is and its applications in calculating the values. Sometimes, you only have to work on some rows or some columns. That’s where the axis parameter comes in handy.
Check the post above to see when to use axis=0 and axis=1.