Separate columns by sequential N rows in python

I have a dataframe with two columns, I need to separate these two columns (column A and B) by N sequential rows (for example 100 rows). so the output will be 100 rows in column A and B, another 100 rows in column C and D, …..is there a specific function can deal with this purpose?

Answer

Assuming n always evenly divides the length of the frame, vsplit + hstack is an option:

n = 5
new_df = pd.DataFrame(np.hstack(np.vsplit(df.values, len(df) // n)))
new_df.columns = new_df.columns.map(lambda c: chr(c + ord('A')))

Complete Working Example:

import numpy as np
import pandas as pd

df = pd.DataFrame({'A': np.arange(1, 16),
                   'B': np.arange(101, 116)})

n = 5
new_df = pd.DataFrame(np.hstack(np.vsplit(df.values, len(df) // n)))
new_df.columns = new_df.columns.map(lambda c: chr(c + ord('A')))
print(new_df)
     A    B
0    1  101
1    2  102
2    3  103
3    4  104
4    5  105
5    6  106
6    7  107
7    8  108
8    9  109
9   10  110
10  11  111
11  12  112
12  13  113
13  14  114
14  15  115

new_df:

   A    B   C    D   E    F
0  1  101   6  106  11  111
1  2  102   7  107  12  112
2  3  103   8  108  13  113
3  4  104   9  109  14  114
4  5  105  10  110  15  115