Pandas AttributeError: 'DataFrame' object has no attribute 'concat' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Pandas AttributeError: 'DataFrame' object has no attribute 'concat' (/thread-39437.html) |
Pandas AttributeError: 'DataFrame' object has no attribute 'concat' - Sameer33 - Feb-17-2023 Dears I am trying to merge multiple excel files into a single file with Python, but I get an error and I can't fix it. My Code is import os import pandas as pd cwd = os.path.abspath('') files = os.listdir(cwd) folder = r"C:\Users\Sameer\Downloads\Sales" ## Method 1 gets the first sheet of a given file df = pd.DataFrame() for file in files: if file.endswith('.xlsx'): df = df.append(pd.read_excel(file), ignore_index=True) df.head() df.to_excel('total_sales.xlsx') ## Method 2 gets all sheets of a given file df_total = pd.DataFrame() for file in files: # loop through Excel files if file.endswith('.xlsx'): excel_file = pd.ExcelFile(file) sheets = excel_file.sheet_names for sheet in sheets: # loop through sheets inside an Excel file df = excel_file.parse(sheet_name = sheet) df_total = df_total.append(df) df_total.to_excel('combined_file.xlsx')
RE: I need help !!!! - Yoriz - Feb-17-2023 The error line 24 df_total = df_total.concat(df)does not match your posted code line 24 df = excel_file.parse(sheet_name = sheet)neither does the next line df_total = df_total.append(df) DataFrame does not have a concat methodPandas has a concat functionhttps://pandas.pydata.org/docs/reference/api/pandas.concat.html Wrote:pandas.concat(objs, *, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True) RE: I need help !!!! - deanhystad - Feb-17-2023 In your next post you will compose a meaningful topic that describes your question, not "I need help!!!!". I usually ignore posts with meaningless topics like "I need help", "What is wrong", "Python problem". "Error concatenating dataframes" would be a good topic for this thread. concat is a function in pandas, just like read_excel(). concat() is not a method of DataFrame. You would concat df and df_total like this. df_total = pd.concat((df_total, df))Pandas append is depreciated. Do not use it. Instead of this: cwd = os.path.abspath('') files = os.listdir(cwd) df = pd.DataFrame() for file in files: if file.endswith('.xlsx'): df = df.append(pd.read_excel(file), ignore_index=True)Use something like this: import pandas as pd from pathlib import Path files = Path('.').glob("*.xlsx") df = pd.concat((pd.read_excel(file) for file in files)) print(df)I think pathlib is far superior to using os tools. RE: I need help !!!! - Sameer33 - Feb-17-2023 (Feb-17-2023, 03:35 PM)Yoriz Wrote: pandas.concat(objs, *, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True) Thanks to you Can you, however, write the entire correct code? I'm still learning the Python programming language. RE: I need help !!!! - deanhystad - Feb-17-2023 You won't learn by having others write code for you. I am confident that this is within your abilities. If you cannot make it work after having put in some good effort, come back and post what you have. RE: I need help !!!! - Sameer33 - Feb-17-2023 Thank you all very much for your assistance. |