Search code examples
pythonexcelpandasmultiple-columnsmulti-index

Mix of columns in excel to one colum with Pandas


I have to import this Excel in code and I would like to unify the multi-index in a single column. I would like to delete the unnamed columns and unify everything into one. I don't know if it's possible.

Excel table

I have tried the following and it imports, but the output is not as expected. I add the code here too

import pandas as pd
import numpy as np

macro = pd.read_excel(nameExcel, sheet_name=nameSheet, skiprows=3, header=[1,3,4])
macro = macro[macro.columns[1:]]
macro

Code tried


Solution

  • The way to solve it is to save another header of the same length as the previous header.

    cols = [...]
    if len(df1.columns) == len(cols):
       df1.columns = cols
    else:
       print("error")