Python Forum

Full Version: Rolling window and apply code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
please show what you have coded so far
(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 :)
(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.
(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.. :)
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.
I hope this helps you, help me.
Thanks!