简体   繁体   English

从多个Excel文件创建熊猫数据框

[英]Creating Pandas Data Frame from Multiple Excel Files

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. 除了我想从Excel文件列表中读取数据。 I have a list of filenames called 'filenames' that I want to merge into a single dataframe. 我有一个名为“ filenames”的文件名列表,我想合并到一个数据框中。

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 我的代码应:-获取8个Excel文件的列表-将每个Excel文件加载到相应的数据框中,并将这些数据框存储在新列表中-将整个数据框列表合并为一个数据框-打印出新的大数据框-保存新的大数据框到Excel文件

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. 很抱歉,它没有给我任何错误消息-它似乎没有在打印数据帧或将其保存到Excel。

Any help would be gratefully received 任何帮助将不胜感激

Don't you mean to say PandasOperations().MergetoSingleDF() instead of PandasOperations.MergetoSingleDF, as you currently write? 您不是要说PandasOperations()。MergetoSingleDF()而不是您当前编写的PandasOperations.MergetoSingleDF吗? Also, your class member functions such as def MergeToSingleDF() should accept self as first argument. 另外,您的类成员函数(例如def MergeToSingleDF())应接受self作为第一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM