Calculate a rolling statistic like a rolling average
Dataframe created with Pandas. The rolling() method allows you to calculate rolling windows. The idea of calculating a rolling window is most commonly employed in signal processing and time-series data. To put it another way, we take a window of size k at a time and apply some mathematical operation to it. A window of size k signifies that k successive values are displayed at the same time. All of the ‘k’ values are equally weighted in the simplest instance. In the below example window size is 5.
Python3
# importing pandas import pandas as pd from datetime import datetime # reading csv file data = pd.read_csv( 'covid_data.csv' ) # converting string data to datetime data[ 'ObservationDate' ] = pd.to_datetime(data[ 'ObservationDate' ]) data[ 'Last Update' ] = pd.to_datetime(data[ 'Last Update' ]) # setting index data = data.set_index( 'ObservationDate' ) data = data[[ 'Last Update' , 'Confirmed' ]] data[ 'rolling_sum' ] = data.rolling( 5 ). sum () print (data.head()) |
Output:
Last Update Confirmed rolling_sum ObservationDate 2020-01-22 2020-01-22 17:00:00 1.0 NaN 2020-01-22 2020-01-22 17:00:00 14.0 NaN 2020-01-22 2020-01-22 17:00:00 6.0 NaN 2020-01-22 2020-01-22 17:00:00 1.0 NaN 2020-01-22 2020-01-22 17:00:00 0.0 22.0
Manipulating Time Series Data in Python
A collection of observations (activity) for a single subject (entity) at various time intervals is known as time-series data. In the case of metrics, time series are equally spaced and in the case of events, time series are unequally spaced. We may add the date and time for each record in this Pandas module, as well as fetch dataframe records and discover data inside a specific date and time range.
Contact Us