Articles

aggregate mean in pandas dataframe with example

aggregate mean in pandas dataframe with example


In this pandas tutorial, we will discuss about:

  • aggregate mean in pandas,
  • aggregate mean example pandas dataframe

Before we move ahead with aggregate mean in pandas, lets first learn to create a dataframe. The dataframe will be used throughout our discussion on this topic.

 

Pandas DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe. The data can be a dictionary that stores list of values with specified key.

 

Example: Create a Dataframe in pandas

In this pandas example, we will create a dataframe with 4 rows and 3 columns with some data and assign indices through index parameter.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: Created dataframe is shown below

       length  breadth  area
one       5.6     12.9    20
two       7.8      4.5    56
three     4.5     21.5    43
four      5.3      6.0    45

Now lets use the dataframe to find aggregate mean in pandas.


Aggregate mean in pandas

If we want to get the average value of the columns in the dataframe, then we have to use aggregate functions.

 

In that, mean is one of the function, which will return average value (row/column) of the dataframe.

 

For aggregation, the method used is agg() and for average, the method used is mean.

There are three scenarios of using this function. Let's discuss one by one.


Scenario - 1 : mean aggregation over the rows

In this scenario, we will get the aggregate mean in pandas or average over each row in the dataframe.

Syntax:

dataframe.agg('mean')

where, dataframe is the input dataframe.

 

Exampleaggregate mean in pandas over row example

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#mean aggregation over the rows
data.agg('mean')

Output: In this aggregate mean in pandas code, we will get average among all rows.

length      5.800
breadth    11.225
area       41.000
dtype: float64

Lets see another scenario on aggregate mean in pandas.


Scenario - 2 : mean aggregation per column

In this scenario, we will get the average for the mentioned columns in the dataframe.

Syntax:

dataframe.agg({'column' : 'mean', ..............})

where, dataframe is the input dataframe and column is the column name to get average.

 

Example: aggregate mean example pandas dataframe per column

In this example, we will get average value in length and area columns.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#mean aggregation per column
data.agg({'length' : 'mean', 'area' : 'mean'})

OutputIn this aggregate mean in pandas code, we will get average of all values in length and area column.

length     5.8
area      41.0
dtype: float64

Lets see third scenario on aggregate mean in pandas.


Scenario - 3 : mean aggregation over the columns

In this scenario, we will get the average value over each column in the dataframe.

Syntax:

dataframe.agg("mean", axis="columns")

where, dataframe is the input dataframe.

 

Example: aggregate mean example pandas dataframe over columns

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# aggregation over the columns
data.agg("mean", axis="columns")

OutputIn this aggregate mean example pandas dataframe code, we will get the average over the columns.

one      12.833333
two      22.766667
three    23.000000
four     18.766667
dtype: float64

This wraps up our session on aggregate mean in pandas using various techniques.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1000+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 08,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!