-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

2 Answers2

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)
Timeless
  • 22,580
  • 4
  • 12
  • 30
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)