Python Forum
Appending a row of data in an MS Excel file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appending a row of data in an MS Excel file
#1
I have the following working code which amalgamates excel file data into one Excel file. How do I amend the code so that I do not have an index column on the first column?

import pandas as pd
import os

input_location = "D:/Power BI & Python/Test - Input File/"
output_location = "D:/Power BI & Python/Test - Output File/"
output_file = "Consolidated.xlsx"

excelfiles = os.listdir(input_location)

excelfilesdf = pd.DataFrame()

for excelfile in excelfiles:
    if excelfile.endswith('xlsx'):
        dfFilename = {'sales_id':excelfile}  # to place the MS Excel filename in the first column, use the first column header
        df = pd.read_excel(input_location + excelfile)
        excelfilesdf = excelfilesdf.append(dfFilename, ignore_index=True)
        excelfilesdf = excelfilesdf.append(df)

excelfilesdf.to_excel(output_location + output_file)
Reply
#2
Read the documentation for DataFrame.to_excel(). There is an argument that controls if the row index names are written to the file.

While you're at it you should take a look at glob. Would be useful for what you are doing (finding all files with extension .xlsx).

https://docs.python.org/3/library/glob.html
Reply
#3
(Nov-05-2022, 08:27 PM)azizrasul Wrote: How do I amend the code so that I do not have an index column on the first column?
Like this.
excelfilesdf.to_excel(output_location + output_file, index=False)
Reply
#4
Thanks both. I was using index=False but unfortunately in the wrong line, that's why it gave me an error.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Automated filler for an Excel form is not writing the data Quian34 2 36 Jun-10-2026, 07:27 AM
Last Post: Larz60+
  docx file to pandas dataframe/excel iitip92 1 5,689 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  Python openyxl not updating Excel file MrBean12 1 3,949 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 2,383 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 5,058 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 2,968 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 5,359 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 3,090 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 9,775 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 1,988 Feb-16-2023, 08:11 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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