moving average in pandas in matlab

You can use the rolling method in Pandas to calculate the moving average. Here's an example code:

main.py
import pandas as pd

# create sample data
data = {'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
df = pd.DataFrame(data)

# calculate moving average with window size of 3
df['rolling_avg'] = df['value'].rolling(window=3).mean()

print(df)
234 chars
11 lines

In this example, we first create a DataFrame with a value column containing our sample data. We then use the rolling method to apply a window function to the value column, and in this case, we use the mean function to calculate the average of each window. The window parameter specifies the size of the window. Finally, we store the values in a new column rolling_avg.

You can adjust the window size based on your needs.

related categories

gistlibby LogSnag