Keep multi-columns name in a concat operation

Viewed 19

I have many dataframes that looks like that :

enter image description here

I did something like that but I have to specified skiprows=[0] or 1 to get the result.

path = r'/Users/jacques/Library/Mobile Documents/com~apple~CloudDocs/Projets/data/'
files = [file for file in Path(path).rglob('Base*.xlsx')]

df = pd.DataFrame()
for file in files:
    data = pd.read_excel(file,                     
                    sheet_name=None, 
                    skiprows=[1]
                    )
    for key in data.keys():
        df1 = data[key]
        res_df = pd.concat(df1[[date, X]].rename(columns={date: "date"})
            for date, X in zip(df1.columns[::2], df1.columns[1::2])).pivot_table(index="date")
        df = pd.concat([df, res_df],
                        axis=1)

df.columns = df.columns.str.replace('Index','').str.strip().str.replace(' ', '_').str.lower()
df = df.T.drop_duplicates().T
df.index = pd.to_datetime(df.index)

df.head()

I would like also to keep the 2 lines in the columns name and have the same result as in excel (but with one date column)

0 Answers
Related