3

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

G.Peach
  • 59
  • 2
  • 8
  • what is `xl_out` supposed to do? That's probably empty and resets the path of xlwt_writer to None or the empty string. – miraculixx Jul 18 '16 at 13:48
  • I got that from reading various other help pages: http://stackoverflow.com/questions/28058563/write-to-stringio-object-using-pandas-excelwriter I have changed the xl_out to the path that the file is in & now it has an issue with the my_writer.save() line. It gives me the error: "in save f = open(file_name_or_filelike_obj, 'w+b') IOError: [Errno 13] Permission denied: 'C:\\Users\\Georgina\\Documents\\Software Development\\Bacti Comments Auto-sort' " – G.Peach Jul 18 '16 at 14:12
  • I have permissions for that directory according to windows exporer – G.Peach Jul 18 '16 at 14:38
  • how was this solved? I have the same problem in windows 10 – Dev_Man Jul 29 '17 at 08:13

1 Answers1

1

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()
privod
  • 209
  • 5
  • 7