Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas - Window Functions



Window functions in Pandas provide a powerful way to perform operations on a series of data, allowing you to compute statistics and other aggregations over a window of data points. These functions are particularly useful in time series analysis and other situations where you need to consider a range of data points around each observation.

Pandas supports four main types of windowing operations −

  • Rolling Window: A sliding window that can be fixed or variable in size.

  • Weighted Window: A non-rectangular, weighted window supplied by the scipy.signal library.

  • Expanding Window: An accumulating window that includes all data points up to the current one.

  • Exponentially Weighted Window: An accumulating window that applies exponential weighting to previous data points.

We will now learn how each of these can be applied on DataFrame objects.

Rolling Window

A rolling window operation involves moving a fixed-size window across the data, performing an aggregation function (like sum or mean) within that window. It is very flexible and supports various time-based operations.

To perform this operation we can use therolling() function. This function can be applied on a series of data. Specify thewindow=n argument and apply the appropriate statistical function on top of it. Therolling() function returns thepandas.typing.api.Rolling object.

Example

Following is the example of applying the rolling window operation on DataFrame using therolling() function.

import pandas as pdimport numpy as npdf = pd.DataFrame(np.random.randn(10, 4),   index = pd.date_range('1/1/2000', periods=10),   columns = ['A', 'B', 'C', 'D'])print(df.rolling(window=3).mean())

Itsoutput is as follows −

                    A           B           C           D2000-01-01        NaN         NaN         NaN         NaN2000-01-02        NaN         NaN         NaN         NaN2000-01-03   0.434553   -0.667940   -1.051718   -0.8264522000-01-04   0.628267   -0.047040   -0.287467   -0.1611102000-01-05   0.398233    0.003517    0.099126   -0.4055652000-01-06   0.641798    0.656184   -0.322728    0.4280152000-01-07   0.188403    0.010913   -0.708645    0.1609322000-01-08   0.188043   -0.253039   -0.818125   -0.1084852000-01-09   0.682819   -0.606846   -0.178411   -0.4041272000-01-10   0.688583    0.127786    0.513832   -1.067156

Note: Since the window size is 3, for first two elements there are nulls and from third the value will be the average of then,n-1 andn-2 elements. Thus we can also apply various functions as mentioned above.

Weighted Window

A weighted window applies a non-rectangular window function, often used in signal processing. Thewin_type parameter is used to specify the window type, which corresponds to a window function from thescipy.signal library.

Example

This example demonstrates applying the weighted window operation on the Pandas Series object.

import pandas as pd# Creating a seriess = pd.Series(range(10))# Applying a triangular weighted windowresult = s.rolling(window=5, win_type="triang").mean()print(result)

When we run above program, it produces following result −

0    NaN1    NaN2    NaN3    NaN4    2.05    3.06    4.07    5.08    6.09    7.0dtype: float64

Expanding Window

An expanding window calculates the aggregation statistic over all the data points available up to the current point, allowing for cumulative calculations.

Theexpanding() function can be applied on a series of data. Specify themin_periods=n argument and apply the appropriate statistical function on top of it. This function returns apandas.typing.api.Expanding object.

Example

Here is an example of applying the expanding window operation on the DataFame object.

import pandas as pdimport numpy as npdf = pd.DataFrame(np.random.randn(10, 4),   index = pd.date_range('1/1/2000', periods=10),   columns = ['A', 'B', 'C', 'D'])print(df.expanding(min_periods=3).mean())

Itsoutput is as follows −

                    A           B           C           D2000-01-01        NaN         NaN         NaN         NaN2000-01-02        NaN         NaN         NaN         NaN2000-01-03   0.434553   -0.667940   -1.051718   -0.8264522000-01-04   0.743328   -0.198015   -0.852462   -0.2625472000-01-05   0.614776   -0.205649   -0.583641   -0.3032542000-01-06   0.538175   -0.005878   -0.687223   -0.1992192000-01-07   0.505503   -0.108475   -0.790826   -0.0810562000-01-08   0.454751   -0.223420   -0.671572   -0.2302152000-01-09   0.586390   -0.206201   -0.517619   -0.2675212000-01-10   0.560427   -0.037597   -0.399429   -0.376886

Exponentially Weighted Window

This type of windowing operation applies exponential weighting to previous data points, which means that older data points are given progressively less importance.

Theewm() function is applied on a series of data. Specify any of the com, span,halflife argument and apply the appropriate statistical function on top of it. It assigns the weights exponentially. This function returnspandas.typing.api.ExponentialMovingWindow object.

import pandas as pdimport numpy as np df = pd.DataFrame(np.random.randn(10, 4),   index = pd.date_range('1/1/2000', periods=10),   columns = ['A', 'B', 'C', 'D'])print(df.ewm(com=0.5).mean())

Itsoutput is as follows −

                    A           B           C           D2000-01-01   1.088512   -0.650942   -2.547450   -0.5668582000-01-02   0.865131   -0.453626   -1.137961    0.0587472000-01-03  -0.132245   -0.807671   -0.308308   -1.4910022000-01-04   1.084036    0.555444   -0.272119    0.4801112000-01-05   0.425682    0.025511    0.239162   -0.1532902000-01-06   0.245094    0.671373   -0.725025    0.1633102000-01-07   0.288030   -0.259337   -1.183515    0.4731912000-01-08   0.162317   -0.771884   -0.285564   -0.6920012000-01-09   1.147156   -0.302900    0.380851   -0.6079762000-01-10   0.600216    0.885614    0.569808   -1.110113

Window functions are majorly used in finding the trends within the data graphically by smoothing the curve. If there is lot of variation in the everyday data and a lot of data points are available, then taking the samples and plotting is one method and applying the window computations and plotting the graph on the results is another method. By these methods, we can smooth the curve or the trend.

Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp