.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples\pandas\plot_format01.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_pandas_plot_format01.py: 01. Sliding window ============================================ .. GENERATED FROM PYTHON SOURCE LINES 5-63 .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Data: feature_0 feature_1 feature_2 feature_3 feature_4 feature_5 feature_6 feature_7 feature_8 feature_9 patient day 0 0.782839 0.579171 0.875666 0.896433 0.522666 0.424833 0.019499 0.843256 0.508497 0.023651 0 -15 1 0.938406 0.686556 0.716277 0.474455 0.839579 0.552715 0.597967 0.308887 0.613897 0.567229 0 -14 2 0.917493 0.842386 0.728984 0.522605 0.850795 0.407061 0.984394 0.082715 0.594869 0.059661 0 -13 3 0.722237 0.947399 0.549326 0.725588 0.976665 0.596684 0.882883 0.594011 0.760227 0.505716 0 -12 4 0.680577 0.052478 0.142805 0.977845 0.755283 0.091267 0.700849 0.692678 0.687108 0.046146 0 -11 ... ... ... ... ... ... ... ... ... ... ... ... ... 1495 0.668256 0.162212 0.051996 0.768991 0.126166 0.594980 0.606882 0.973683 0.444446 0.438552 99 -5 1496 0.074663 0.669059 0.815666 0.425611 0.900212 0.069323 0.597965 0.300225 0.087881 0.892852 99 -4 1497 0.572242 0.856386 0.745518 0.044273 0.610881 0.320131 0.804878 0.652734 0.014684 0.986622 99 -3 1498 0.266470 0.857181 0.888199 0.421400 0.169874 0.498681 0.395948 0.240385 0.194971 0.757166 99 -2 1499 0.131035 0.197843 0.184269 0.605561 0.850481 0.687066 0.508127 0.683124 0.304230 0.779854 99 -1 [1500 rows x 12 columns] C:\Users\kelda\Desktop\repositories\github\python-spare-code\main\examples\pandas\plot_format01.py:50: FutureWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning. Result: feature_0 feature_1 feature_2 feature_3 feature_4 feature_5 feature_6 feature_7 feature_8 feature_9 patient day window patient 0 0 0.782839 0.579171 0.875666 0.896433 0.522666 0.424833 0.019499 0.843256 0.508497 0.023651 0 -15 0 1 0.938406 0.686556 0.716277 0.474455 0.839579 0.552715 0.597967 0.308887 0.613897 0.567229 0 -14 0 2 0.917493 0.842386 0.728984 0.522605 0.850795 0.407061 0.984394 0.082715 0.594869 0.059661 0 -13 0 1 0.938406 0.686556 0.716277 0.474455 0.839579 0.552715 0.597967 0.308887 0.613897 0.567229 0 -14 1 2 0.917493 0.842386 0.728984 0.522605 0.850795 0.407061 0.984394 0.082715 0.594869 0.059661 0 -13 1 ... ... ... ... ... ... ... ... ... ... ... ... ... ... 99 1497 0.572242 0.856386 0.745518 0.044273 0.610881 0.320131 0.804878 0.652734 0.014684 0.986622 99 -3 13 1498 0.266470 0.857181 0.888199 0.421400 0.169874 0.498681 0.395948 0.240385 0.194971 0.757166 99 -2 13 1497 0.572242 0.856386 0.745518 0.044273 0.610881 0.320131 0.804878 0.652734 0.014684 0.986622 99 -3 14 1498 0.266470 0.857181 0.888199 0.421400 0.169874 0.498681 0.395948 0.240385 0.194971 0.757166 99 -2 14 1499 0.131035 0.197843 0.184269 0.605561 0.850481 0.687066 0.508127 0.683124 0.304230 0.779854 99 -1 14 [3900 rows x 13 columns] | .. code-block:: default :lineno-start: 6 # Interesting code. # np.lib.stride_tricks.sliding_window_view(df.index, 3) # Libraries import numpy as np import pandas as pd # Configuration ROWS, COLS = 1500, 10 PATIENTS = 100 # Create random values features = np.random.random_sample((ROWS, COLS)) patients = np.random.randint(PATIENTS, size=(ROWS,1)) # Create DataFrame df = pd.DataFrame(data=features) df = df.add_prefix('feature_') df['patient'] = patients df['day'] = -(df.groupby('patient').cumcount()+1) df = df.sort_values(by=['patient', 'day'], ascending=[True, True]).reset_index(drop=True) # Show print("\nData:") print(df) # ---------------------------------- # Method I: Own method # ---------------------------------- def sliding_window_iter(series, size, include_id=True): """series is a column of a DataFrame. .. note: The DataFrame should be pre-ordered to ensure that IDs remain consistent. """ for i, start_row in enumerate(range(len(series) - size + 1)): s = series[start_row:start_row + size] if include_id: s['window'] = i yield s # Group by patient and compute sliding window result = df.groupby('patient').apply(lambda x: pd.concat(sliding_window_iter(x, 3))) # Show print("\nResult:") print(result) # ---------------------------------- # Method II: Using rolling # ---------------------------------- #a = df.groupby('patient').rolling(window=3) #b = [win for win in a if win.shape[0] == 3] #c = pd.concat(b) #print(c) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.276 seconds) .. _sphx_glr_download__examples_pandas_plot_format01.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_format01.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_format01.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_