'Ho do I combine multiple sheets in excel files into one csv file with mappings in Python?
im trying to combine excel files in a directory into one csv. All the current excel files have same number of columns and same headers. file 1 df- sheet1
State Region Brand Coverage Type Initial Effective Date Issue
GA S gg Out 8/1/2012 Need Records
OH NE gg Out 7/31/2021 Pre-existing
file 2 df- sheet2
State Region Brand Coverage Type Initial Effective Date Issue
CA S gg Out 8/1/2012 Need Records
IA NE gg Out 7/31/2021 Pre-existing
I want my final combined csv to look like
Rownumber Sheetname Filename State Region Brand Coverage Type Initial Effective Date Issue
1 2 df2 CA S gg Out 8/1/2012 Need
2 2 df2 IA NE gg Out 7/31/2021 Pre-
The row number should reflect exact row number from sheet they belong to. How do I do this so that all future excel files created in the directory append to a combined csv file in the above format? currently there are 2 csv files but soon there will be more added with the same columns and headers. I want this code to pick up the excel files and append to the final combined csv like this. Will setup a task scheduler for the same. I attempted the below but it is only giving me an empty df-
import os
import pandas as pd
cwd = os.path.abspath('')
files = os.listdir(cwd)
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_csv('combined_file.csv')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
