.. 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.089311 0.669843 0.636815 0.691866 0.438045 0.670048 0.647059 0.497548 0.132073 0.064704 0 -16 1 0.623224 0.100191 0.283330 0.066976 0.857515 0.064504 0.488081 0.188542 0.935137 0.399247 0 -15 2 0.267652 0.650595 0.954580 0.596319 0.656430 0.402114 0.682637 0.800231 0.439874 0.132753 0 -14 3 0.115091 0.043677 0.184747 0.912493 0.917703 0.712375 0.027196 0.315069 0.217029 0.985370 0 -13 4 0.463474 0.822237 0.095631 0.567447 0.096011 0.676549 0.013525 0.840770 0.524598 0.781150 0 -12 ... ... ... ... ... ... ... ... ... ... ... ... ... 1495 0.644658 0.758443 0.591858 0.916131 0.765378 0.127884 0.800184 0.587909 0.180938 0.664073 99 -5 1496 0.844910 0.702561 0.366853 0.425155 0.324503 0.809659 0.159465 0.539452 0.753092 0.523524 99 -4 1497 0.024136 0.754523 0.429183 0.917456 0.971045 0.790479 0.403902 0.017655 0.146382 0.338187 99 -3 1498 0.018974 0.699461 0.258756 0.544835 0.256932 0.162095 0.980543 0.010086 0.950997 0.523899 99 -2 1499 0.266844 0.339369 0.333165 0.636019 0.217694 0.624248 0.679772 0.384634 0.203819 0.520705 99 -1 [1500 rows x 12 columns] 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.089311 0.669843 0.636815 0.691866 0.438045 0.670048 0.647059 0.497548 0.132073 0.064704 0 -16 0 1 0.623224 0.100191 0.283330 0.066976 0.857515 0.064504 0.488081 0.188542 0.935137 0.399247 0 -15 0 2 0.267652 0.650595 0.954580 0.596319 0.656430 0.402114 0.682637 0.800231 0.439874 0.132753 0 -14 0 1 0.623224 0.100191 0.283330 0.066976 0.857515 0.064504 0.488081 0.188542 0.935137 0.399247 0 -15 1 2 0.267652 0.650595 0.954580 0.596319 0.656430 0.402114 0.682637 0.800231 0.439874 0.132753 0 -14 1 ... ... ... ... ... ... ... ... ... ... ... ... ... ... 99 1497 0.024136 0.754523 0.429183 0.917456 0.971045 0.790479 0.403902 0.017655 0.146382 0.338187 99 -3 14 1498 0.018974 0.699461 0.258756 0.544835 0.256932 0.162095 0.980543 0.010086 0.950997 0.523899 99 -2 14 1497 0.024136 0.754523 0.429183 0.917456 0.971045 0.790479 0.403902 0.017655 0.146382 0.338187 99 -3 15 1498 0.018974 0.699461 0.258756 0.544835 0.256932 0.162095 0.980543 0.010086 0.950997 0.523899 99 -2 15 1499 0.266844 0.339369 0.333165 0.636019 0.217694 0.624248 0.679772 0.384634 0.203819 0.520705 99 -1 15 [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.531 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 `_