| by Arround The Web | No comments

Pandas Rolling Mean

In Python, the “Pandas” library provides/offers various operations to manage and manipulate data. It can also be utilized to determine/calculate the rolling mean of the time series. The “Rolling Mean” is represented as the moving average, and is utilized to determine the rolling window calculation. To determine the rolling mean in Python, the “rolling()” method of the “Pandas” module is used along with the “mean()” method.

This Python article will deliver a detailed tutorial on how to determine the rolling mean via the below content:

How to Determine the Rolling Mean of Pandas DataFrame in Python?

To determine the rolling mean of Pandas DataFrame, the “DataFrame.rolling()” is used along with the “mean()” method in Python. The “DataFrame.rolling()” method calculates rolling window calculations on a Pandas DataFrame, and the mean() method calculates the total mean for each rolling.

A rolling window calculation is a statistical operation that is performed on a moving window of data. The window size is specified/indicated by the “window” parameter. Here is the syntax:

DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None, step=None, method='single')

Now let’s understand this method using the below examples:

Example 1: Calculating the Rolling Mean of a Single Column in Pandas DataFrame

Here is the code to determine the rolling mean of a single DataFrame column:

import pandas
df = pandas.DataFrame({'A': [15, 20, 32, 42, 41, 55, 54],
                       'B': [32, 31, 12, 42, 18, 21, 42],
                       'C': [43, 10, 32, 12, 32, 52, 50],
                       'D': [11, 21, 32, 66, 32, 12, 11]})
print(df,'\n')
df['rolling'] = df['A'].rolling(3).mean()
print(df)

In the above code:

  • The “pandas” library is imported, and a Pandas “DataFrame” is created with the four columns.
  • The “rolling()” method takes the window size “3” as an argument and is used along with the “mean()” method to calculate the mean of the “A” column, but only for the last 3 values.
  • The new column “rolling” is created in the DataFrame.

Output

The above output shows the rolling column that includes the rolling mean of the “A” column with a window size of “3”.

Note: The rolling mean of the first three values in column “A” is (15 + 20 + 32) / 3 = 22.33, and the rolling mean of the next three values is (20 + 32 + 42) / 3 = 31.33, and so on.

Example 2: Calculating the Rolling Mean of Multiple Columns in Pandas DataFrame

Let’s overview the below example for evaluating the rolling mean of multiple DataFrame columns:

import pandas
df = pandas.DataFrame({'A': [15, 20, 32, 42, 41, 55, 54],
                       'B': [32, 31, 12, 42, 18, 21, 42],
                       'C': [43, 10, 32, 12, 32, 52, 50],
                       'D': [11, 21, 32, 66, 32, 12, 11]})
print(df,'\n')
df['rolling_A'] = df['A'].rolling(5).mean()
df['rolling_D'] = df['D'].rolling(5).mean()
print(df)

In the above code:

  • The “df.rolling()” method is used multiple times with the “mean()” method to determine the Pandas rolling mean of multiple columns named “A” and “D”.

Output

The rolling mean of multiple columns has been calculated successfully.

Example 3: Calculating the Rolling Mean of Entire Pandas DataFrame

The below code is used to determine the rolling mean of the entire DataFrame:

import pandas
df = pandas.DataFrame({'A': [15, 20, 32, 42, 41, 55, 54],
                       'B': [32, 31, 12, 42, 18, 21, 42],
                       'C': [43, 10, 32, 12, 32, 52, 50],
                       'D': [11, 21, 32, 66, 32, 12, 11]})
print(df,'\n')
output = df.rolling(4).mean()
print(output)

Here in this code:

  • The “df.rolling()” takes the mean window parameter value “4” as an argument and is used along with the “mean()” method to determine the rolling mean of the entire DataFrame.

Output

The rolling mean of the entire DataFrame has been determined.

Conclusion

In Python, the “DataFrame.rolling()” is used along with the “mean()” method to determine/calculate the rolling mean of Pandas DataFrame. This Python article has provided a detailed guide on determining the rolling mean of a single column, multiple columns, and the entire Pandas DataFrame using suitable examples.

Share Button

Source: linuxhint.com

Leave a Reply