Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write DataFrame to excel along with the axis name

I’m trying to write a dataframe along with the axis name to an excel file.

df = pd.DataFrame({'350p HP': [7844, 2, 1],
                'tc206':[1721.5, 0.44, 1.81],
                'tcs208':[77.52, 0.02, 0.11], 
                'sas22':[1330.8, 0.34, 0.58],
                'tss21':[3070.2, 0.78, 0.56],
                'tas22': [5619.2, 1.43, 1.03]}, index=['DP', 'DP/REP', 'AF'])
df.rename_axis('col name', axis=1, inplace=True)

here is the dataframe I’m trying to write with the axis name col name. I tried with both engine='openpyxl' and engine='xlsxwriter'.

`excel_file = "test.xlsx"
 writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
 df.to_excel(writer, sheet_name='axisname')
 writer.save()`

I prefer to make it work with xlsxwriter as there are other dependencies. I’m trying to find a solution for this. Please let me know if there is a way to do this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can pass df.columns.name to parameter index_label:

 df.to_excel(writer, index_label=df.columns.name)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading