简体   繁体   中英

Inserting a row above pandas column headers to save a title name in the first cell of csv / excel file

I have multiple dataframes that look like this, the data is irrelevant.

在此处输入图像描述

I want it to look like this, i want to insert a title above the column headers.

在此处输入图像描述

I want to combine them into multiple tabs in an excel file.

Is it possible to add another row above the column headers and insert a Title into the first cell before saving the file to excel.

I am currently doing it like this.

with pd.ExcelWriter('merged_file.xlsx',engine='xlsxwriter') as writer:
    for filename in os.listdir(directory):
        if filename.endswith('xlsx'):
            print(filename)
            if 'brands' in filename:
                some function
            elif 'share' in filename:
                somefunction
            else:
                some function
            df.to_excel(writer,sheet_name=f'{filename[:-5]}',index=True,index_label=True)
writer.close()

But the sheet_name is too long, that's why I want to add the title above the column headers.

I tried this code,

columns = df.columns
columns = list(zip([f'{filename[:-5]}'] * len(df.columns), columns))             
columns = pd.MultiIndex.from_tuples(columns) 
df2 = pd.DataFrame(df,index=df.index,columns=columns) 
df2.to_excel(writer,sheet_name=f'{filename[0:3]}',index=True,index_label=True)

But it ends up looking like this with all the data gone,

在此处输入图像描述

It should look like this

在此处输入图像描述

You can write data from sedond row first and then write to first cell your text:

df = pd.DataFrame({'col': list('abc'), 'col1': list('def')})
print (df)
  col col1
0   a    d
1   b    e
2   c    f

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', startrow = 1, index=False)
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

text = 'sometitle'
worksheet.write(0, 0, text)
writer.save()

Then for reading need:

title = pd.read_excel('test.xlsx', nrows=0).columns[0]
print (title)
sometitle

df = pd.read_excel('test.xlsx', skiprows=1)
print (df)
  col col1
0   a    d
1   b    e
2   c    f

You can use MultiIndex . There is an example:

import pandas as pd 
df = pd.read_excel('data.xls')
header = pd.MultiIndex.from_product([['Title'],
                                     list(df.columns)])
pd.DataFrame(df.to_numpy(), None , columns = header)

Also, I can share with you my solution with real data in Deepnote (my favorite tool). Feel free to duplicate and play with your own.xls: https://deepnote.com/publish/3cfd4171-58e8-48fd-af21-930347e8e713

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM