0

I want to iterate through the dataFrame and write chunks of every 8th row to a new dataframe, like:

for index, row in pdDataset.iterrows():
pdDataset_full.append(pdDataset.loc[index:index+8])

I got the correct output when i print the loop, but ended up with a empty DataFrame

print(len(pdDataset_full))
0
Shalem
  • 1,446
  • 2
  • 22
  • 47
Yako
  • 1
  • `df = df.append()` ? – furas Feb 10 '20 at 14:56
  • First, you do not assign the `append` call. Second, it is ill-advised to [call `DataFrame.append` in a loop](https://stackoverflow.com/a/36489724/1422451). Finally, this feels like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Please tell us the fuller background of *x* problem and not asking for help with *y* solution of appending every 8 rows. – Parfait Feb 10 '20 at 14:57
  • it looks like job for [rolling window](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html) – furas Feb 10 '20 at 14:59

1 Answers1

0

append() is immutable. It does not change the DataFrame but returns a new DataFrame with the row appended.

Try :

pdDataset_full = pdDataset_full.append(pdDataset.loc[index:index+8])
SUN
  • 181
  • 5
  • Sorry, but I am not sure about to write chunks of 8 every row to a new Dataframe. what exactly you want to achieve. – SUN Feb 10 '20 at 15:16