Quantcast
Viewing all articles
Browse latest Browse all 14185

How to generate rolling subsequences into a dataframe in Python

I have the following data frame:|col0||----||1 ||2 ||3 ||4 ||5 ||... ||1000|

I'd like roll col0 into a data frame with a window size of 5, so the outcome would be like this:

col0col1col2...col995
123...996
234...997
345...998
456...999
567...1000

col0 .... col9951 2 3 ... 9962 3 4 ... 9973 4 5 ... 9984 5 6 ... 9995 6 7 ... 1000

I've tried using loops and "iloc" which would produce correct results, but as the original data frame gets much longer, it would take too long to finish. Is there any way to do it faster, more efficiently in Python?

Thank you.


Viewing all articles
Browse latest Browse all 14185

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>