JackB
JackB

Reputation: 31

How to add a specific row in a pandas dataframe multiple times

I have a dataframe that's 13 columns by 557 rows. I've exported the data to excel, but I want to add the first row (the column names) every 56 rows so that when I print it, the header is at the top.

I've tried a bunch of things like .concat and .append but I'm a beginner with Pandas so I'm not very good

I have this near the end of my code to add the header, I have the right names in the list but it just messes everything up

header = []
for title in order:
    header.append(title)
panda.iloc[2] = header

Note that header = ['Age', 'RK', 'Team', ...]

Upvotes: 1

Views: 311

Answers (1)

piRSquared
piRSquared

Reputation: 294278

np.split

Assume k is the number of lines you want per split

df = pd.DataFrame(1, range(20), [*'ABC'])

k = 5
print(*np.split(df, range(k, len(df), k)), sep='\n\n')

   A  B  C
0  1  1  1
1  1  1  1
2  1  1  1
3  1  1  1
4  1  1  1

   A  B  C
5  1  1  1
6  1  1  1
7  1  1  1
8  1  1  1
9  1  1  1

    A  B  C
10  1  1  1
11  1  1  1
12  1  1  1
13  1  1  1
14  1  1  1

    A  B  C
15  1  1  1
16  1  1  1
17  1  1  1
18  1  1  1
19  1  1  1

Upvotes: 3

Related Questions