Pandas is a great python library for manipulating data in the dataset. To do so there are many functions in it and performs manipulation. Suppose you have a numeric dataset or dataframe and want to find the average over the entire or specific column of the dataset then you can do so using pandas. In this entire tutorial, you will know how to calculate the average of columns in pandas with steps.
Steps to Calculate the average of the column in pandas
In this section, you will know all the steps required to find the average of the column in pandas.
Step 1: Import library
The first step is to import all the necessary libraries for implementation. I am using only pandas library so let’s import it. In python, you can import packages using the import statement.
import pandas as pd
Step 2: Create a Dummy dataframe
The second step is to create a sample dataframe where you will find the average. Make sure the dataset should contain numeric records on at least one column, otherwise the average will not be calculated.
data = {
"name":["Sahil","Abhishek","Dan","Rob","Maya"],
"col1":[10,20,30,40,50],
"col2":[100,20,50,60,70],
}
df = pd.DataFrame(data)
print(df
Output

Step 3: Find the average of the column
Now there are many ways you can find the average of columns in pandas.
Method 1: Using mean() function
The first method is using the pandas mean() method. Let’s find the average using this method.
If you will apply mean() on the entire dataframe then it will find mean for the entire numeric column in the dataframe.
df.mean()
Output

Suppose you want to find the average for a particular column then you can do so by applying the mean on that particular column. For example, I want to find the average for the col1 then I will add the following line of code.
df["col1"].mean()
Output

In the same way, you can find the average for the other columns.
Method 2 : describe() function
The second method is to find the average of the dataframe is using the describe() method. If you will use this function then it will find entire stats over the dataframe like min, max, average, deviation e.t.c.
Let’s apply this function.
Applying describe()method on the entire dataframe
If you will apply describe() method on the entire dataframe then It will find stats for the entire numerical columns of the dataframe.
df.describe()
Output

Applying describe() method on the entire Single column
In the same way, you can find the states or averages of a single column. You have just use describe() function on that column name.
df["col1"].describe()
Output

You can use the same process for finding the average of the other columns.
Conclusion
Pandas is a very popular python package for manipulating datasets. These are the methods to find the average of the column in pandas. I hope you have fully understood this tutorial. If you have any doubt then you can contact us for more help and information
Source:
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.