pythonpandas

How to concatenate n rows of content, to current row, in a rolling window, in pandas?


I'm looking to transform a dataframe containing

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

into [[1, 2, 3, []], [4, 5, 6, [1, 2, 3, 4, 5, 6]], [7, 8, 9, [4, 5, 6, 7, 8, 9]], [10, 11, 12, [7, 8, 9, 10, 11, 12]]]

So far the only working solution I've come up with is:

import pandas as pd
import numpy as np

# Create the DataFrame
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]))

# Initialize an empty list to store the result
result = []

# Iterate over the rows in the DataFrame
for i in range(len(df)):
    # If it's the first row, append the row with an empty list
    if i == 0:
        result.append(list(df.iloc[i]) + [[]])
    # If it's not the first row, concatenate the current and previous row
    else:
        current_row = list(df.iloc[i])
        previous_row = list(df.iloc[i-1])
        concatenated_row = current_row + [previous_row + current_row]
        result.append(concatenated_row)

# Print the result
print(result)

Is there no build in Pandas function that can roll a window, and add the results to current row, like the above can?


Solution

  • This doesn't need windowing, IIUC, you can use df.shift:

    x = df.apply(lambda x: x.tolist(), axis=1)
    df[3] = (x.shift() + x)
    

    Output:

        0   1   2                      3
    0   1   2   3                    NaN
    1   4   5   6     [1, 2, 3, 4, 5, 6]
    2   7   8   9     [4, 5, 6, 7, 8, 9]
    3  10  11  12  [7, 8, 9, 10, 11, 12]
    

    Adding window sizing:

    import pandas as pd
    import numpy as np
    from functools import reduce
    
    df = pd.DataFrame(np.arange(99).reshape(-1,3))
    x = df.apply(lambda x: x.tolist(), axis=1)
    
    #change window size here
    window_size = 3 
    
    df[3] = reduce(lambda x, y: x+y, [x.shift(i) for i in range(window_size,-1,-1)])
    
    df
    

    Output:

         0   1   2                                                 3
    0    0   1   2                                               NaN
    1    3   4   5                                               NaN
    2    6   7   8                                               NaN
    3    9  10  11            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
    4   12  13  14         [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
    5   15  16  17      [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
    6   18  19  20   [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
    7   21  22  23  [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
    8   24  25  26  [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
    9   27  28  29  [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
    10  30  31  32  [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
    11  33  34  35  [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
    12  36  37  38  [27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38]
    13  39  40  41  [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41]
    14  42  43  44  [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44]
    15  45  46  47  [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]
    16  48  49  50  [39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
    17  51  52  53  [42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53]
    18  54  55  56  [45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56]
    19  57  58  59  [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
    20  60  61  62  [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62]
    21  63  64  65  [54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65]
    22  66  67  68  [57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68]
    23  69  70  71  [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71]
    24  72  73  74  [63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74]
    25  75  76  77  [66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77]
    26  78  79  80  [69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
    27  81  82  83  [72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83]
    28  84  85  86  [75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86]
    29  87  88  89  [78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
    30  90  91  92  [81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92]
    31  93  94  95  [84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95]
    32  96  97  98  [87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98]