O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Performing a moving-average calculation

The moving average of a stock can be calculated using .rolling().mean(). The moving average will give you a sense of the performance of a stock over a given time-period, by eliminating "noise" in the performance of the stock. The larger the moving window, the smoother and less random the graph will be, but at the expense of accuracy.

The following sample calculates the moving average for MSFT over 30- and 90-day periods, using the daily close value. The difference in the reduction of noise can be easily determined from the visual:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required