I would like to flat every n rows in a pandas dataframe, e.g., if n=2 then an example would be like
df = pd.DataFrame([[1,2], [3,4], [5,6], [7,8], [9,10], [11,12]])
df.columns = ['a', 'b']
target_df = pd.DataFrame([[1,2, 3, 4], [5,6, 7, 8], [9,10, 11, 12]])
target_df.columns = ['a1', 'b1', 'a2', 'b2']
print(df, '\n\n', target_df)
a b
0 1 2
1 3 4
2 5 6
3 7 8
4 9 10
5 11 12
a1 b1 a2 b2
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
Is there any fast way to do so? Note that the length of the dataframe and n could be arbitrarily large, so hardcoding n is not a good option.