简体   繁体   中英

Pandas to_excel to write a dataframe results in blank spreadsheet - how write to excel using xlwt?

Writing a dataframe:

New_DF = 
            caviae  species  murliniae  freundii  braakii  freundii  cloacae  \
15368485     NaN      NaN        NaN       NaN      NaN       NaN      NaN   
15368486     NaN      NaN        NaN         1      NaN         1      NaN   
15368487     NaN      NaN        NaN       NaN      NaN       NaN      NaN   
15368488     NaN      NaN        NaN       NaN      NaN       NaN      NaN   
15368489     NaN        1        NaN       NaN      NaN       NaN      NaN  

etc to a .xls spreadsheet, workbook 'Sheet1':

import StringIO
import pandas as pd
import xlwt

New_DF = pd.DataFrame(SpeciesCount,labNo,Species)

xlwt_writer = pd.io.excel.get_writer('xlwt')

my_writer = xlwt_writer('C:\Users\Georgina\Documents\Test\1291707 STS Excel Extract.xls')

xl_out = StringIO.StringIO()
my_writer.path = xl_out
New_DF.to_excel(my_writer,sheet_name='Sheet1',startrow = 4)
my_writer.save()

This doesn't alter the .xls file or output an error message

Any help much appreciated

Try this:

import StringIO
import pandas as pd
import xlwt

New_DF = pd.DataFrame(SpeciesCount,labNo,Species)

writer = pd.ExcelWriter('C:\Users\Georgina\Documents\Test\1291707 STS Excel Extract.xls', engine='xlwt')
New_DF.to_excel(writer, sheet_name='Sheet1', startrow = 4)
writer.save()

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