Reputation: 183
I'm new to Python, know just enough R to get by. I have a 10 by 10 dataframe.
small2
USLC USSC INTD ... DSTS PCAP PRE
0 0.059304 0.019987 -0.034140 ... 0.003009 0.113144 -0.021656
1 0.003835 -0.024248 0.012446 ... 0.005323 -0.013716 0.011109
2 -0.045045 -0.047186 -0.002372 ... -0.011956 -0.118342 -0.045023
3 0.054108 0.002787 0.003714 ... 0.014466 0.128931 -0.007596
4 0.064045 0.111250 0.077478 ... 0.012059 0.115427 0.079145
5 0.041442 0.042858 0.047701 ... 0.009984 0.047098 0.003579
6 0.081832 0.046531 0.010531 ... 0.031772 0.126552 0.001398
7 -0.047171 0.022883 -0.065095 ... -0.010224 -0.025990 -0.055431
8 0.054844 0.073193 0.044514 ... 0.016301 0.031755 0.044597
9 -0.032403 -0.043930 -0.065013 ... 0.011944 -0.032902 -0.117689
I want to create a list of several dataframes that are each just rolling 5 by 10 frames. Rows 0 through 4, 1 through 5, etc. I've seen articles addressing something similar, but they haven't worked. I'm thinking about it like lapply
in R.
I've tried splits = [small2.iloc[[i-4:i]] for i in small2.index]
and got a syntax error from the colon.
I then tried splits = [small2.iloc[[i-4,i]] for i in small2.index]
which gave me a list of ten elements. It should be six 5 by 10 elements.
Feel like I'm missing something basic. Thank you!
Upvotes: 1
Views: 47
Reputation: 13457
You can also use the DataFrame.rolling
method for this and simply toss away the first few iterations until the window reaches its full size
splits = [frame for frame in small2.rolling(5) if len(frame) == 5]
Upvotes: 1
Reputation: 183
I figured it out. splits = [small2.iloc[i-4:i+1] for i in small2.index[4:10]]
Not sure how this indexing makes sense though.
Upvotes: 1