Rolling windows in pandas based on a condition

Question:

I’ve a dataset which looks like:

column
0
0
1
2
2

I need to create windows, iterating over a list containing the window size.
The output should be

  • when window size = 1
dataset1
0
0
dataset2
1
dataset3
2
2
  • when window size = 2
dataset1
0
0
1
dataset2
1
2
2

the pseudo code should be

df is a dataframe
windows = [1,2,3]
for window in windows:
   create multiple rolling subdataset

EDIT

Unfortunately, I’m creating windows with different size, so the code looks like that

windows = [1,2,3]
for window in windows:
  L = [g for k,g in dataf.groupby('day')]
  dfs = [pd.concat(L[i:i+window]) for i in range(0, len(L)-window+1)]

Unfortunately, dfs will only store the dataframe created when windows == 3.
I need to access all the dataframe because I need to add a column for each.

I tried to store the dfs into a dictionary whose keys where the window size, but it tells that "dictionary is unhashable"

Asked By: gingerbread_54

||

Answers:

You can use:

L = [g for k,g in df.groupby('Index')]

n = 2
dfs = [pd.concat(L[i:i+n]) for i in range(0, len(L)-n+1)]

print(dfs)

output:

[   Index
 0      0
 1      0
 2      1,
    Index
 2      1
 3      2
 4      2]
update: multiple windows
windows = [1,2,3]
dfs = []
for window in windows:
  L = [g for k,g in dataf.groupby('day')]
  dfs.extend([pd.concat(L[i:i+window]) for i in range(0, len(L)-window+1)])
print(dfs)
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.