1

I have a dataset that looks like this

43466   1323.507803
43467   1396.948621
43468   1481.437362
43469   1611.111671
43470   1379.217261
43471   1425.450351

I am trying to loop through the dataset with Python Pandas and set x and y axis for each day to look at the last 30, here I use a smaller set for shorter explanation - last 3 days

I have itertupled through the rows correctly but I am not sure why this is not working.

I am using

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

dataset = pd.read_csv('~/Desktop/test2.csv')
df = pd.DataFrame(dataset)
for row in dataset.head(2).itertuples():
    #print(row.Date)
    print(dataset.loc[dataset["Date"]==row.Date].tail(5)) 

What I currently get is:

  Date  Usage
0  43466.0   1323.507803
  Date  Usage
1  43467.0   1396.948621

If I loop through the row - I am expecting each print out to start where the date is == to the row.Date being looped. The final print should look like this

row index 0 print

43466   1323.507803

row index 1 print

43466   1323.507803
43467   1396.948621

row index 2 print

43466   1323.507803
43467   1396.948621
43468   1481.437362

. . . . . all the way to row index 5 print

43466   1323.507803
43467   1396.948621
43468   1481.437362
43469   1611.111671
43470   1379.217261
43471   1425.450351
enavuio
  • 1,428
  • 2
  • 19
  • 31
  • Using iterrows (or any other iter* function) in pandas is a bad idea. Take a look here: https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas/55557758#55557758 – cs95 May 18 '19 at 16:45
  • @cs95 I do prefer for loops (vectorization, never knew it was called that). Guess when I was reviewing how to loop through a ds in python (c# devloper) these iter functions were everwhere - I will try to accomplish through for loops then. – enavuio May 18 '19 at 16:54
  • 1
    Why don’t you just use slicing? df.iloc[-30:] – Ben Pap May 18 '19 at 17:08

0 Answers0