To calculate the moving average of a time series data in pandas, we can use the rolling
method which creates a rolling window of the desired time frame and applies a function to the data. In this case, we will apply the mean
function to the window to calculate the moving average.
Here's an example:
main.py423 chars17 lines
Output:
main.py126 chars8 lines
In the example above, we create a dataframe with a time series data and set the date column as the index (the set_index
method with inplace=True
modifies the dataframe in place). We then specify a window size of 3 and calculate the rolling average using the rolling
method with the mean
function applied to the window. The resulting dataframe contains the moving average values.
gistlibby LogSnag