Python Forum
Rolling window and apply code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Rolling window and apply code (/thread-37733.html)



Rolling window and apply code - JunkBoy - Jul-14-2022

Hey
I have many large data frames that I need to split into several new data frames.
they need to be split in rolling window split. In this case the window is 12. But it can change.

FOr instance I Now have 250 row that I split into data frames like this... as you can see this is a very long end repetitive way to do this.. I believe there must be a smarter way to do this.

      #df1 = df_all_sales.iloc[0:12].. df2 = df_all_sales.iloc[1:13].. and on and on
        df1 = pd.DataFrame(df_all_sales, columns=['time', 'sales-transaction']).iloc[0:12]
        df2 = pd.DataFrame(df_all_sales, columns=['time', 'sales-transaction']).iloc[1:13]
        df3 = pd.DataFrame(df_all_sales, columns=['time', 'sales-transaction'])iloc[2:14]
        df4 = pd.DataFrame(df_all_sales, columns=['time', 'sales-transaction']).iloc[3:15]
I need help to simplify and reduce the code to something so I can easily access the new Dfs.
Maybe make a for loop or something that can simplify this.
Thanks for advice!


RE: Split DF to alot of new DFs - Larz60+ - Jul-15-2022

please show what you have coded so far


RE: Split DF to alot of new DFs - JunkBoy - Jul-15-2022

(Jul-15-2022, 05:22 AM)Larz60+ Wrote: please show what you have coded so far

I have what is above. I am not sure how to do anything else..
So I am hoping for help :)


RE: Split DF to alot of new DFs - perfringo - Jul-15-2022

(Jul-14-2022, 06:00 PM)JunkBoy Wrote: I need help to simplify and reduce the code to something so I can easily access the new Dfs.

I suggest to read Why you don't want to dynamically create variables and then (re)consider your options.


RE: Split DF to alot of new DFs - JunkBoy - Jul-16-2022

(Jul-15-2022, 10:16 PM)perfringo Wrote:
(Jul-14-2022, 06:00 PM)JunkBoy Wrote: I need help to simplify and reduce the code to something so I can easily access the new Dfs.

I suggest to read Why you don't want to dynamically create variables and then (re)consider your options.

Thanks.
The thing is that I am moving my code from excel to python. I am very new to python and do not have the knowledge to really know what to consider as options in this case. I need advice or someone to set me on the right track.. :)


RE: Split DF to alot of new DFs - deanhystad - Jul-16-2022

Quote:I have many large data frames that I need to split into several new data frames.
they need to be split in rolling window split. In this case the window is 12. But it can change.
Why do you think you need a bunch of data frames? What ae you going to do with these data frames? Please describe what you are trying to accomplish instead of how.


RE: Split DF to alot of new DFs - JunkBoy - Jul-23-2022

I hope this helps you, help me.
Thanks!