Taiko
Taiko

Reputation: 51

Merging two rows in pandas to one header cell per column

i have a table in pandas that looks like this:

0 A Another
1 header header
2 First row
3 Second row

and what i would like to have a table like this :

0 A header Another header
1 First row
2 Second row

how can i merge cells 0 and 1 to one header column?

Upvotes: 1

Views: 1220

Answers (1)

Pierre D
Pierre D

Reputation: 26201

There is a question about that column '0', if indeed it is one. But if it is not (and is just the index having been pasted slightly incorrectly), then I would do:

newdf = df.iloc[1:].set_axis(df.columns + ' ' + df.iloc[0], axis=1)
>>> newdf
  A header Another header
1    First            row
2   Second            row

Upvotes: 2

Related Questions