Q:

Appending Multiple Rows Of Pandas DataFrame into New DataFrame

I have a current data frame as below :
   F1 , F2 , F3 , F4 , Label
   1  , 2  , 3  , 4  ,  Dog 
   2  , 3  , 4  , 5  ,  Cat
   3  , 4  , 5  , 6  ,  Cat
   4  , 5  , 6  , 7  ,  Dog 
   5  , 6  , 7  , 8  ,  Cat
   6  , 7  , 8  , 9  ,  Dog

               .
               . 
               . 
               . 

   1  , 2  , 3  , 4  ,  Dog

What I want is to iterate over the dataframe and combine next 3 rows into one row and then right to another dataframe so that I have the following output :
   F1 , F2 , F3 , F4 , Label , F1 , F2 , F3 , F4 , Label , F1 , F2 , F3 , F4 , Label
   1  , 2  , 3  , 4  ,  Dog  , 2  , 3  , 4  , 5  , Cat   , 3  , 4  , 5  , 6  ,  Cat
   2  , 3  , 4  , 5  ,  Cat  , 3  , 4  , 5  , 6  , Cat   , 4  , 5  , 6  , 7  ,  Dog
   3  , 4  , 5  , 6  ,  Cat  , 4  , 5  , 6  , 7  , Dog   , 5  , 6  , 7  , 8  ,  Cat
   4  , 5  , 6  , 7  ,  Dog  , 5  , 6  , 7  , 8  , Cat   , 6  , 7  , 8  , 9  ,  Dog

I know that the last 2 rows will not have NaN values but that doesnt matter as I can always drop them later.
The code I have is as below :
import pandas as pd
import numpy as np

path = r'C:\Users\Ahmed Ismail Khalid\Desktop\Research Paper\Training and Validation.csv'

df = pd.read_csv(path)
cols = ['Positive Score','Compound Score','Negative Score','Neutral Score','Class Label',
        'Positive Score','Compound Score','Negative Score','Neutral Score','Class Label',
        'Positive Score','Compound Score','Negative Score','Neutral Score','Class Label',
        'Positive Score','Compound Score','Negative Score','Neutral Score','Class Label',
        'Positive Score','Compound Score','Negative Score','Neutral Score','Class Label',
        'Positive Score','Compound Score','Negative Score','Neutral Score','Class Label',
        'Positive Score','Compound Score','Negative Score','Neutral Score','Class Label']
new_df = pd.DataFrame(columns=cols)

rows = []

for index, row in df.items() :
    row_m = df.iloc[index:index+6]
    rows.append(row_m)

new_df = pd.concat(rows,axis=1)

print(new_df)

Any and all help would be appreciated.
Thanks in advance

A:

I think you are looking for something like this.
subdf = [df.iloc[i:i+len(df)-2].reset_index(drop=True) for i in range(3)]
ddf = pd.concat(subdf, axis=1)
print(ddf)

If df is the dataframe you gave as an example (I dropped the comma separator and the last line) the above code prints:
   F1  F2  F3  F4 Label  F1  F2  F3  F4 Label  F1  F2  F3  F4 Label
0   1   2   3   4   Dog   2   3   4   5   Cat   3   4   5   6   Cat
1   2   3   4   5   Cat   3   4   5   6   Cat   4   5   6   7   Dog
2   3   4   5   6   Cat   4   5   6   7   Dog   5   6   7   8   Cat
3   4   5   6   7   Dog   5   6   7   8   Cat   6   7   8   9   Dog

And no need to drop NaN values if you stop the selection at the correct line (that's the purpose of -2 in i+len(df)-2).
Note also the use of reset.index(drop=True): the sliced dataframes need to  forget about their original indexes otherwise pd.concat later will attach each row at its original index. drop=True prevents from adding a column index holding the original indexes.

