0

I have this massive dataframe which has 3 different columns of values under each one heading.

As an example, first it looked something like this:

|   | 0    | 1    | 2    | 3    | ..
| 0 | a    | 7.3  | 9.1  | NaN  | ..
| 1 | b    | 2.51 | 4.8  | 6.33 | ..
| 2 | c    | NaN  | NaN  | NaN  | ..
| 3 | d    | NaN  | 3.73 | NaN  | ..

1, 2 and 3 all belong together. For simplicity of the program I used integers for the dataframe index and columns.

But now that it finished calculating stuff, I changed the columns to the appropriate string.

|   | 0    | Heading 1 | Heading 1 | Heading 1 | ..   
| 0 | a    | 7.3       | 9.1       | NaN       | ..
| 1 | b    | 2.51      | 4.8       | 6.33      | ..
| 2 | c    | NaN       | NaN       | NaN       | ..
| 3 | d    | NaN       | 3.73      | NaN       | ..

Everything runs perfectly smooth up until this point, but here's where I'm stuck.

All I wanna do is merge the 3 "Heading 1" into one giant cell, so that it looks something like this:

|   | 0    |        Heading 1       | ..
| 0 | a    | 7.3    | 9.1    | NaN  | ..
| 1 | b    | 2.51   | 4.8    | 6.33 | ..
| 2 | c    | NaN    | NaN    | NaN  | ..
| 3 | d    | NaN    | 3.73   | NaN  | ..

But everything I find online is merging the entire column, values included.

I'd really appreciate if someone could help me out here!

Lauruva
  • 11
  • 3
  • Maybe one of those answers can help: https://stackoverflow.com/questions/53700965/pandas-to-excel-merged-header-column or https://stackoverflow.com/questions/27420263/pandas-parse-merged-header-columns-from-excel – Kcode Jan 23 '22 at 20:33
  • 1
    There is not really a concept of merged cells in pandas. This can happen for the display with MultiIndexes. What are you really trying to do? – mozway Jan 23 '22 at 20:35
  • @mozway Well I'm trying to format the excel, basically. I wrote the whole thing to be time-efficient and reduce workload on myself, so ideally it leaves as little work for me as possible. Of course I can manually format it, but I thought there might be a way to automate it. – Lauruva Jan 23 '22 at 21:02

0 Answers0