Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting extra column while converting multiple excel '.xlsx' to '.csv' files in python?

i am trying to convert multiple excel file '.xlsx' to '.csv' using pandas in python. i am able to convert multiple excel file in csv but i am getting an extra column at the beginning of '.csv' file.

here is my code-

import pandas as pd,xlrd,glob

excel_files = glob.glob(r"C:\Users\Videos\file reader\*.xlsx")
for excel_file in excel_files:

  print("Converting '{}'".format(excel_file))
  try:
      df = pd.read_excel(excel_file)
      output = excel_file.split('.')[0]+'.csv'
      df.to_csv(output)
  except KeyError:
      print("  Failed to convert")

input-

enter image description here

output-

enter image description here

As we can see in output file there is an extra column. can anyone show me how can i remove it ?

Thanks

like image 828
Sat.N Avatar asked Oct 19 '25 12:10

Sat.N


1 Answers

set df.to_csv(output,index=False)

full code:

import pandas as pd,xlrd,glob

excel_files = glob.glob(r"C:\Users\Videos\file reader\*.xlsx")
for excel_file in excel_files:

  print("Converting '{}'".format(excel_file))
  try:
      df = pd.read_excel(excel_file)
      output = excel_file.split('.')[0]+'.csv'
      df.to_csv(output,index=False)
  except KeyError:
      print("  Failed to convert")
like image 172
anky Avatar answered Oct 22 '25 02:10

anky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!