1

I have a dataframe with 3000 rows, sorted by date, and 5 columns.

I need to slice this dataframe in several ones, of 251 days, using a loop. So the first one will have from day 1 to day 251, the 2nd one from day 2 to 252, and so on.

Then, I want to save it, so then I can iterate a function over it.

Can you please help me? Thanks in advance.

[SOLVED]

1 Answer 1

2

You can create a list of these dataframes this way:

step = 250
dataframe_list = [dataframe[i:i + step] for i in range(len(dataframe) - step)]

if you want to use this in a loop only once, you can use a generator to use less memory.

dataframe_generator = (dataframe[i:i + step] for i in range(len(dataframe) - step))
Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Axel, Thank you for your help. I tried your suggestion, but it happens to create list from row 1 to 249 and then from 250 to 499, instead of 1 to 249, 2 to 250, 3 to 251 and so on. Here's what I wrote with your suggestion: step = 250 prices_list = [prices[i:i + step] for i in range(0, len(prices), step)]
Oh sorry. Corrected.
Perfect, it worked. Thank you. Is there any way to refer directly to, for instance, the 45th iteration?
With the first solution, of course: dataframe_list[45], but with the generator, it is not possible (or you have to do a loop with the next function, very inefficient). If you need it, you can just write dataframe[45:45 + step]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.