The meaning of axis in Pandas
This tutorial explains axis
the meaning of the parameters used in various methods of Pandas objects like DataFrames and Series.
import pandas as pd
empl_df = pd.DataFrame(
{
"Name": ["Jon", "Willy", "Mike", "Luna", "Sam", "Aliza"],
"Age": [30, 33, 35, 30, 30, 31],
"Weight(KG)": [75, 75, 80, 70, 73, 70],
"Height(meters)": [1.7, 1.7, 1.85, 1.75, 1.8, 1.75],
"Salary($)": [3300, 3500, 4000, 3050, 3500, 3700],
}
)
print(empl_df)
Output:
Name Age Weight(KG) Height(meters) Salary($)
0 Jon 30 75 1.70 3300
1 Willy 33 75 1.70 3500
2 Mike 35 80 1.85 4000
3 Luna 30 70 1.75 3050
4 Sam 30 73 1.80 3500
5 Aliza 31 70 1.75 3700
We use DataFrame to explain how to use parameters empl_df
in Pandas methods .axis
axis
Using parameters in Pandas methods
axis
The parameter specifies the direction in which to apply a particular method or function on a DataFrame. axis=0
represents that the function is applied column-wise, and axis=1
represents that the function is applied row-wise on a DataFrame.
If we apply the function column-wise, we will get a single-row result; if we apply the function row-wise, we will get a single-column DataFrame.
Example: Using in Pandas methodsaxis=0
import pandas as pd
empl_df = pd.DataFrame(
{
"Name": ["Jon", "Willy", "Mike", "Luna", "Sam", "Aliza"],
"Age": [30, 33, 35, 30, 30, 31],
"Weight(KG)": [75, 75, 80, 70, 73, 70],
"Height(meters)": [1.7, 1.7, 1.85, 1.75, 1.8, 1.75],
"Salary($)": [3300, 3500, 4000, 3050, 3500, 3700],
}
)
print("The Employee DataFrame is:")
print(empl_df, "\n")
print("The DataFrame with mean values of each column is:")
print(empl_df.mean(axis=0))
Output:
The Employee DataFrame is:
Name Age Weight(KG) Height(meters) Salary($)
0 Jon 30 75 1.70 3300
1 Willy 33 75 1.70 3500
2 Mike 35 80 1.85 4000
3 Luna 30 70 1.75 3050
4 Sam 30 73 1.80 3500
5 Aliza 31 70 1.75 3700
The DataFrame with mean values of each column is:
Age 31.500000
Weight(KG) 73.833333
Height(meters) 1.758333
Salary($) 3508.333333
dtype: float64
It calculates empl_df
the column-wise mean of a DataFrame. The mean is calculated only for columns that have values.
If we set axis=0
, it will calculate the mean of each column by averaging the row values of that column.
Example using Pandas methodaxis=1
import pandas as pd
empl_df = pd.DataFrame(
{
"Name": ["Jon", "Willy", "Mike", "Luna", "Sam", "Aliza"],
"Age": [30, 33, 35, 30, 30, 31],
"Weight(KG)": [75, 75, 80, 70, 73, 70],
"Height(meters)": [1.7, 1.7, 1.85, 1.75, 1.8, 1.75],
"Salary($)": [3300, 3500, 4000, 3050, 3500, 3700],
}
)
print("The Employee DataFrame is:")
print(empl_df, "\n")
print("The DataFrame with mean values of each row is:")
print(empl_df.mean(axis=1))
Output:
The Employee DataFrame is:
Name Age Weight(KG) Height(meters) Salary($)
0 Jon 30 75 1.70 3300
1 Willy 33 75 1.70 3500
2 Mike 35 80 1.85 4000
3 Luna 30 70 1.75 3050
4 Sam 30 73 1.80 3500
5 Aliza 31 70 1.75 3700
The DataFrame with mean values of each row is:
0 851.6750
1 902.4250
2 1029.2125
3 787.9375
4 901.2000
5 950.6875
dtype: float64
It calculates empl_df
the row mean of the DataFrame, in other words, it will calculate the mean of each row by averaging the values of the numeric columns of that row. We will get a single column at the end for the mean of each row.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:107 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa
Pandas DataFrame DataFrame.mean() function
Publish Date:2025/04/30 Views:85 Category:Python
-
Python Pandas DataFrame.mean() function calculates the mean of the values of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No
Pandas DataFrame DataFrame.isin() function
Publish Date:2025/04/30 Views:133 Category:Python
-
The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains values the value specified in the input . pandas.DataFrame.isin(values) grammar DataFrame . isin(values) parameter values iterable - lis
Pandas DataFrame DataFrame.groupby() function
Publish Date:2025/04/30 Views:161 Category:Python
-
pandas.DataFrame.groupby() takes a DataFrame as input and divides the DataFrame into groups based on a given criterion. We can use groupby() the method to easily process large datasets. pandas.DataFrame.groupby() grammar DataFrame . groupby
Pandas DataFrame DataFrame.fillna() function
Publish Date:2025/04/30 Views:60 Category:Python
-
The pandas.DataFrame.fillna() function replaces the values DataFrame in NaN with a certain value. pandas.DataFrame.fillna() grammar DataFrame . fillna( value = None , method = None , axis = None , inplace = False , limit = None , down
Pandas DataFrame DataFrame.dropna() function
Publish Date:2025/04/30 Views:181 Category:Python
-
The pandas.DataFrame.dropna() function removes null values (missing values) from a DataFrame by dropping rows or columns that contain null values DataFrame . NaN ( Not a Number ) and NaT ( Not a Time ) represent null values. DataFrame
Pandas DataFrame DataFrame.assign() function
Publish Date:2025/04/30 Views:55 Category:Python
-
Python Pandas DataFrame.assign() function assigns new columns to DataFrame . pandas.DataFrame.assign() grammar DataFrame . assign( ** kwargs) parameter **kwargs Keyword arguments, DataFrame the column names to be assigned to are passed as k
Pandas DataFrame DataFrame.transform() function
Publish Date:2025/04/30 Views:120 Category:Python
-
Python Pandas DataFrame.transform() DataFrame applies a function on and transforms DataFrame . The function to be applied is passed as an argument to the function. The axis length of transform() the transformed should be the same as the ori