Pandas concat doesn't work 'DataFrame' object has no attribute 'concat' pandas

Question:

I am trying ton concat excel files with pandas but I get this error message "AttributeError: ‘DataFrame’ object has no attrctionibute ‘concat’"

My code is the following:

def action(): 
    all_files = filedialog.askopenfilename(initialdir = "/", 
        multiple=True,
        title="select",
        filetypes=(
            ("all files", "*.*"),
            ("Excel", "*.xlsx*")))
    dossier=filedialog.askdirectory()
    final19=pd.DataFrame() 
    final18=pd.DataFrame()
    first=True
    for f in all_files:
            step19=pd.read_excel(f,sheet_name='2019')
            step18=pd.read_excel(f,sheet_name='2018')
            if first:  
                first=False
                fina19=step19
                final18=step18
            else:
                final19 = final19.concat(step19)
                final18 = final18.concat(step18)
                final19.to_excel(dossier+'\Filefinal.xlsx',sheet_name='19',index=False)
                final18.to_excel(dossier+'\Filefinal.xlsx',sheet_name='18',index=False)
    tkinter.messagebox.showinfo("Files", "ready")

Thanks a lot by advance!

Asked By: Geunck

||

Answers:

concat is a method from the pandas library, not a class method of a pandas.DataFrame. What that means, is that you can’t use df1.concat(df2), but as stated in the documentation, you need to use it as such:

df_concat = pd.concat([df1, df2])

so in your case, it would look like:

final19 = pd.concat([final19,step19])
final18 = pd.concat([final18,step18])
Answered By: Florent Monin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.