3

Hi so this question is almost exactly like this one:

creating pandas data frame from multiple files

except that I want to read data from a list of Excel files. I have a list of filenames called 'filenames' that I want to merge into a single dataframe.

My code goes:

import tkinter as tk
import pandas as pd
import tkinter.messagebox as tr 

from tkinter.filedialog import askopenfilename      

LARGE_FONT = ("Verdana", 12)

class BlahTest(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        frame = StartPage(container, self)
        self.frames[StartPage] = frame
        frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Add files to start compiling your report", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

         button1 = tk.Button(self, text="Add Files", command=FileOperations.openFile)
        button1.pack()

 class FileOperations():

     def openFile():
        options = {}
    options['initialdir'] = 'C:\\Users\\Blah'
    options['filetypes'] = [('Excel files', '.xlsx')]
    options['multiple'] = 1
    global filenames
    filenames = tk.filedialog.askopenfilename(**options)

    if len(filenames) == 8: #checks if the user has selected exactly 8 files else shows errormessage
        PandasOperations.MergetoSingleDF

    else:
        tr.showerror("Wrong number of files","There should be exactly 8 files")

class PandasOperations():

def MergetoSingleDF():
    df_list = [pd.read_excel((file), sheetname=0) for file in filenames]
    big_df = pd.Dataframe()
    big_df = pd.concat(df_list)

    big_df

    writer = pd.ExcelWriter('C:\\Users\\Blah.xlsx', engine = 'xlsxwriter')
    big_df.to_excel(writer, sheet_name='Patch Summary by Server Report', index=False)
    workbook = writer.book
    worksheet = writer.sheets['Patch Summary by Server Report']
    writer.save()


app = BlahTest()
app.mainloop()

My code should: - get a list of 8 Excel files - load each Excel file into a corresponding dataframe, with those dataframes stored in a new list - merge the whole list of dataframes into one dataframe - print out the new big dataframe - save the new big dataframe to an Excel file

I'm sorry it's not giving me any error messages - it just doesn't seem to be printing the datafrane or saving it to Excel.

Any help would be gratefully received

Community
  • 1
  • 1
Davtho1983
  • 3,827
  • 8
  • 54
  • 105
  • You've not stated why your code isn't working which it looks like it should – EdChum Jul 24 '15 at 11:17
  • This code works for me. what error are you getting? – omri_saadon Jul 24 '15 at 11:22
  • You may want to post http://stackoverflow.com/help/mcve ... – boardrider Jul 24 '15 at 12:57
  • Ok so I changed the code above to show a bit more about what I'm trying to do. I can't seem to get the dataframe big_df to either print or save to an Excel file – Davtho1983 Jul 24 '15 at 13:05
  • whats the error message. please be specific about the error – AZhao Jul 24 '15 at 13:21
  • Ok there is no error message, which is one of the things I am confused about. I will post the whole code at the top – Davtho1983 Jul 24 '15 at 13:29
  • Don't you mean to say PandasOperations().MergetoSingleDF() instead of PandasOperations.MergetoSingleDF, as you currently write? Also, your class member functions such as def MergeToSingleDF() should accept self as first argument. – DonCristobal Jul 24 '15 at 16:55

1 Answers1

0

Don't you mean to say PandasOperations().MergetoSingleDF() instead of PandasOperations.MergetoSingleDF, as you currently write? Also, your class member functions such as def MergeToSingleDF() should accept self as first argument.

DonCristobal
  • 1,965
  • 21
  • 21
  • Ah ok - that makes sense and good call for catching that error, but I'm afraid the code still doesn't seem to work. I think I'm going to need pandastable - or maybe I'll abandon tkinter for the GUI – Davtho1983 Jul 28 '15 at 13:19