Feb-17-2023, 02:05 PM
(This post was last modified: Feb-18-2023, 09:21 AM by Yoriz.
Edit Reason: Added code tags, Title
)
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
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'
Attached Files
