Python Forum
Pandas AttributeError: 'DataFrame' object has no attribute 'concat'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas AttributeError: 'DataFrame' object has no attribute 'concat'
#1
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')
Error:
C:\Users\Sameer\PycharmProjects\merage\venv\Scripts\python.exe C:\Users\Sameer\PycharmProjects\merage\main.py C:\Users\Sameer\PycharmProjects\merage\main.py:11: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. df = df.append(pd.read_excel(file), ignore_index=True) C:\Users\Sameer\PycharmProjects\merage\main.py:11: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. df = df.append(pd.read_excel(file), ignore_index=True) Traceback (most recent call last): File "C:\Users\Sameer\PycharmProjects\merage\main.py", line 24, in <module> df_total = df_total.concat(df) ^^^^^^^^^^^^^^^ File "C:\Users\Sameer\PycharmProjects\merage\venv\Lib\site-packages\pandas\core\generic.py", line 5902, in __getattr__ return object.__getattribute__(self, name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'DataFrame' object has no attribute 'concat'
Yoriz write Feb-17-2023, 03:24 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.xlsx   Sales1.xlsx (Size: 13.14 KB / Downloads: 109)
.xlsx   Sales2.xlsx (Size: 13.25 KB / Downloads: 108)
Reply
#2
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 method
Pandas has a concat function
https://pandas.pydata.org/docs/reference...oncat.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)

Concatenate pandas objects along a particular axis.

Allows optional set logic along the other axes.

Can also add a layer of hierarchical indexing on the concatenation axis, which may be useful if the labels are the same (or overlapping) on the passed axis number.
Reply
#3
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.
Reply
#4
(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.
Reply
#5
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.
Reply
#6
Thank you all very much for your assistance.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 728 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 765 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,671 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  DataFRame.concat() nafshar 3 772 Jul-14-2023, 04:41 PM
Last Post: nafshar
  Question on pandas.dataframe merging two colums shomikc 4 817 Jun-29-2023, 11:30 AM
Last Post: snippsat
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,468 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 741 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,863 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 728 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,325 Apr-15-2023, 05:17 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020