I'm looking for help to merge multiples xlsx files with pandas and tkinter. When I run the code I got this error :
"TypeError: Can only merge Series or DataFrame objects, a <class 'str'> was passed"
Thanks a lot for your help!
My code is the following:
from tkinter import filedialog
import pandas as pd
window = Tk()
window.title("title")
#(etc.)
label .pack()
def action_bouton1():
all_files = filedialog.askopenfilename(
initialdir = "/",
multiple=True,
title="select",
filetypes=(
("all files", "*.*"),
("Excel", "*.xlsx*"),
)
)
saveinfolder=filedialog.askdirectory()
for f in all_files:
step1 =pd.read_excel(f,sheet_name=0,skiprows=1)
merge_files = pd.merge(step1,on='Id',how='outer')
merge_files.to_excel(saveinfolder '\\Synthese.xlsx',index=False)
tkinter.messagebox.showinfo("Files", "Ready")
Thanks!
CodePudding user response:
The merge function takes two dataframes, left and right. You only provide one in the question. In the answer, we separate it with if/else since there is no second df to combine when we read the first file.
final=pd.DataFrame() #all data will stored here.
first=True # first file
for f in all_files:
step1 =pd.read_excel(f,sheet_name=0,skiprows=1)
if first: #if first file don't do anything.
final=step1
first=False
else: #if not first file merge current file and previous final data
final= final.merge(step1,on='Id',how='outer')