I get extra columns when converting multiple excel ‘.xlsx’ files to ‘.csv’ files in python?… here is a solution to the problem.
I get extra columns when converting multiple excel ‘.xlsx’ files to ‘.csv’ files in python?
I’m trying to convert multiple excel files “.xlsx” to “.csv” using pandas in python. I was able to convert multiple excel files in csv, but I got an extra column at the beginning of the “.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")
Enter –
Output –
As we can see in the output file, there is an extra column. Can anyone tell me
How can I remove it?
Thanks
Solution
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")