Search code examples
pythonpandasdataframeconcatenation

How to concat rows(axis=1) with stride?


example:

import pandas as pd

test = {
    't':[0,1,2,3,4,5], 
    'A':[1,1,1,2,2,2], 
    'B':[9,9,9,9,8,8], 
    'C':[1,2,3,4,5,6]
   }

df = pd.DataFrame(test)
df

enter image description here

Tried use window and concat:

window_size = 2

for row_idx in range(df.shape[0] - window_size):
    print(
        pd.concat(
            [df.iloc[[row_idx]],
             df.loc[:, df.columns!='t'].iloc[[row_idx+window_size-1]],
             df.loc[:, df.columns!='t'].iloc[[row_idx+window_size]]],
            axis=1
        )
    )

But get wrong dataframe like this:

enter image description here

Is it possible to use a sliding window to concat data?

enter image description here


Solution

  • pd.concat is alingning indices, so you have to make sure that they fit. You could try the following:

    window_size = 2
    dfs = []
    for n in range(window_size + 1):
        sdf = df.iloc[n:df.shape[0] - window_size + n]
        if n > 0:
            sdf = (
                sdf.drop(columns="t").rename(columns=lambda c: f"{c}_{n}")
                .reset_index(drop=True)
            )
        dfs.append(sdf)
    res = pd.concat(dfs, axis=1)
    

    Result for the sample:

       t  A  B  C  A_1  B_1  C_1  A_2  B_2  C_2
    0  0  1  9  1    1    9    2    1    9    3
    1  1  1  9  2    1    9    3    2    9    4
    2  2  1  9  3    2    9    4    2    8    5
    3  3  2  9  4    2    8    5    2    8    6