Calculate Average of Column in Pandas
Calculate Average of Column in Pandas

Calculate Average of Column in Pandas : Various Ways

GET FREE AMZAON AUDIOBOOKS

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

Sample dataframe for finding average of column in pandas
Sample dataframe for finding the average of the column in pandas

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

finding average for the entire dataframe
finding the average for the entire dataframe

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

finding average for a specific column
finding the average for a specific column

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

finding average for the entire dataframe using describe() function
finding the average for the entire dataframe using describe() function

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

finding average for a specific column using describe() method
finding the average for a specific column using describe() method

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:

Pandas Mean Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner