Python Forum
Rolling window and apply code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rolling window and apply code
#1
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!
Larz60+ write Jul-15-2022, 09:20 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time, please use bbcode tags on future posts.
Reply
#2
please show what you have coded so far
Reply
#3
(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 :)
Reply
#4
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(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.. :)
Reply
#6
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.
Reply
#7
I hope this helps you, help me.
Thanks!
Larz60+ write Jul-18-2022, 09:16 PM:
2nd Notice:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed this time as well, please use bbcode tags.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  apply(pd.Series) until no more array mikisDeWitte 1 2,789 Apr-17-2021, 08:45 PM
Last Post: Caprone
  Hurst Exponent in Rolling Basis illmattic 1 3,868 Jan-06-2021, 09:49 PM
Last Post: illmattic
  Calculating Beta over Rolling Periods illmattic 2 5,422 Sep-27-2020, 11:27 PM
Last Post: Larz60+
  Apply rolling window function over time dimension of 3D data Staph 0 2,199 Jan-01-2020, 08:31 AM
Last Post: Staph
  Grouping data based on rolling conditions kapilan15 0 1,968 Jun-05-2019, 01:07 PM
Last Post: kapilan15
  Pandas .rolling() with some calculations inside irmscher 5 6,232 Apr-04-2019, 11:55 AM
Last Post: scidam
  How to use pandas.rolling mean for 3D input array? Prv_Yadv 1 3,865 Mar-26-2019, 11:49 AM
Last Post: scidam
  Numpy Rolling mean window Thunberd 1 4,833 Jun-14-2018, 01:37 AM
Last Post: Larz60+
  Creating a matrix of rolling variances vvvcvvcv 1 2,773 May-26-2018, 12:51 PM
Last Post: killerrex
  Rolling sum for a window of 2 days (Pandas) klllmmm 1 10,348 Feb-02-2018, 03:24 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020