0

I have a pandas data frame like so:

A  | B  | C  | index
-----------------
a1 | b1 | c1 | 1
a2 | b2 | c2 | 2
a3 | b3 | c3 | 3
a4 | b4 | c4 | 4
a5 | b5 | c5 | 5

This resembles a time series ordered by the index (or date column if you will)

My goal is to create n data frames with each having 3 entries in order. So the solution would be the following in the above example:

df1)

A  | B  | C  | index
-----------------
a1 | b1 | c1 | 1
a2 | b2 | c2 | 2
a3 | b3 | c3 | 3

df2)

A  | B  | C  | index
-----------------
a2 | b2 | c2 | 2
a3 | b3 | c3 | 3
a4 | b4 | c4 | 4

df3)

A  | B  | C  | index
-----------------
a3 | b3 | c3 | 3
a4 | b4 | c4 | 4
a5 | b5 | c5 | 5

How can I do that in python 3.5 and pandas? In other languages I would probably take a for loop and iterate over i in range(1,len(df)-2), but I am guessing there is a much more pythonic solution to this.

Thanks in advance for your help!! :-)

Lukas Hestermeyer
  • 830
  • 1
  • 7
  • 19
  • may be this can help https://stackoverflow.com/questions/14661701/how-to-drop-a-list-of-rows-from-pandas-dataframe – Kallz Aug 22 '17 at 16:56
  • Do you actually need the three data frames, or are you interested in computing aggregated values from each of the three? If the latter, consider using [`rolling()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html). – andrew_reece Aug 22 '17 at 18:59
  • Hi thanks for your answer. I need three data frames as I consider each data frame as one example of a time series to train a machine learning algorithm on it. – Lukas Hestermeyer Aug 22 '17 at 19:31

1 Answers1

3

Still using for loop :

variables = locals()
for i in range(1,4):
    variables["df{0}".format(i)] = df.loc[(df['index']>=i)&(df['index']<=i+2),]
BENY
  • 317,841
  • 20
  • 164
  • 234