Home > database >  How to aggregate n previous rows as list in Pandas DataFrame?
How to aggregate n previous rows as list in Pandas DataFrame?

Time:02-25

As the title says:

a = pd.DataFrame([1,2,3,4,5,6,7,8,9,10])

Having a dataframe with 10 values we want to aggregate say last 5 rows and put them as list into a new column:

>>> a       new_col
    0
0   1
1   2
2   3
3   4
4   5     [1,2,3,4,5]
5   6     [2,3,4,5,6]
6   7     [3,4,5,6,7]
7   8     [4,5,6,7,8]
8   9     [5,6,7,8,9]
9  10     [6,7,8,9,10]

How?

CodePudding user response:

Due to how rolling windows are implemented, you won't be able to aggregate the results as you expect, but we still can reach your desired result by iterating each window and storing the values as a list of values:

>>> new_col_values = [
    window.to_list() if len(window) == 5 else None
    for window in df["column"].rolling(5)
]
>>> df["new_col"] = new_col_values
>>> df
   column           new_col
0       1              None
1       2              None
2       3              None
3       4              None
4       5   [1, 2, 3, 4, 5]
5       6   [2, 3, 4, 5, 6]
6       7   [3, 4, 5, 6, 7]
7       8   [4, 5, 6, 7, 8]
8       9   [5, 6, 7, 8, 9]
9      10  [6, 7, 8, 9, 10]
  • Related