1

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', ...]

3
  • add a sample dataframe and an expected output for a better understanding for us and ppl who will look up to this post later on. Commented May 17, 2019 at 16:00
  • please see this post: stackoverflow.com/questions/15888648/… Commented May 17, 2019 at 16:02
  • Does this need to be done at the pandas level? If you're printing from excel, there are tools to always print the header row that are tolerant of formatting changes, etc. As a bonus, they also keep the header (and row indices, if you care about them) at the top (side) of the field when scrolling. Commented May 17, 2019 at 17:04

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

Could you please explain using * before np
that's the same this as for a in np.split(...): print(a)
I think my explanation was a little unclear, here is a link to my new question [link] stackoverflow.com/questions/56200592/…
@PIG the asterisk operator * at both places before ABC and before np is used for unpacking. You may want to watch youtu.be/sYeqpnAA7U4 for detailed explaination

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.