-2

The code below converts .csv file in C:/Path/ into .xlsx file. However, it creates an extra column when converted to .xlsx file. How can I delete that added extra column? Thank you very much.

import os

for root, dirs, files in os.walk("C:/Processed_Report/", topdown=False)
for name in files:
    base_name, ext = os.path.splitext(name)  #Split name, extension
    if ext in ".csv":
        df = pd.read_csv(os.path.join(root, name))
        df.to_excel(os.path.join(root, 'Test.xlsx'))

Input:

enter image description here

Output:

enter image description here

1
  • 5
    Try with index=False Commented Dec 9, 2022 at 19:12

2 Answers 2

3

You need to pass index=False as a keyword of pandas.DataFrame.to_excel.

Replace this :

df.to_excel(os.path.join(root, 'Test.xlsx'))

By this :

df.to_excel(os.path.join(root, 'Test.xlsx'), index=False)
Sign up to request clarification or add additional context in comments.

3 Comments

I wonder why it defaults to index=True?
No need to answer dupes
@MarkRansom, there has been many discussions about that, github.com/pandas-dev/pandas/issues/…
0

that is just how it is with dataframes in pandas, when you create a dataframe by any mean (like csv file) it ads an extra column which contains the indexes. how ever df to excel function has an argument name index which you can set on False to prevent creating that extra column:

df = pd.read_csv(os.path.join(root, name))
df.to_excel(os.path.join(root, 'Test.xlsx'), index = False)

1 Comment

No need to answer dupes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.