Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating multiple excel worksheets using data in a pandas dataframe

Tags:

python

pandas

Just started using pandas and python.

I have a worksheet which I have read into a dataframe and the applied forward fill (ffill) method to.

I would then like to create a single excel document with two worksheets in it.

One worksheet would have the data in the dataframe before the ffill method is applied and the next would have the dataframe which has had the ffill method applied.

Eventually I intend to create one worksheet for every unique instance of data in a certain column of the dataframe.

I would then like to apply some vba formatting to the results - but i'm not sure which dll or addon or something I would need to call excel vba using python to format headings as bold and add color etc.

I've had partial success in that xlsxwriter will create a new workbook and add sheets, but dataframe.to_excel operations don't seems to work on the workbooks it creates, the workbooks open but the sheets are blank.

Thanks in advance.

import os import time import pandas as pd import xlwt from xlwt.Workbook import * from pandas import ExcelWriter import xlsxwriter  #set folder to import files from path = r'path to some file' #folder = os.listdir(path)  #for loop goes here  #get date date = time.strftime('%Y-%m-%d',time.gmtime(os.path.getmtime(path)))  #import excel document original = pd.DataFrame() data = pd.DataFrame()  original = pd.read_excel(path,sheetname='Leave',skiprows=26) data = pd.read_excel(path,sheetname='Leave',skiprows=26)  print (data.shape) data.fillna(method='ffill',inplace=True)  #the code for creating the workbook and worksheets wb= Workbook() ws1 = wb.add_sheet('original') ws2 = wb.add_sheet('result') original.to_excel(writer,'original') data.to_excel(writer,'result') writer.save('final.xls') 
like image 535
yoshiserry Avatar asked Feb 24 '14 07:02

yoshiserry


1 Answers

import pandas as pd  df1 = pd.DataFrame({'Data': ['a', 'b', 'c', 'd']})  df2 = pd.DataFrame({'Data': [1, 2, 3, 4]})  df3 = pd.DataFrame({'Data': [1.1, 1.2, 1.3, 1.4]})  writer = pd.ExcelWriter('multiple.xlsx', engine='xlsxwriter')  df1.to_excel(writer, sheet_name='Sheeta')  df2.to_excel(writer, sheet_name='Sheetb')  df3.to_excel(writer, sheet_name='Sheetc')  writer.save() 
like image 145
kalyan solasa Avatar answered Oct 23 '22 12:10

kalyan solasa