How To Convert CSV to Excel in Python Using Pandas? (4 Easy Methods)

If you have a dataset in CSV format and want to perform some mathematical operations, then converting them to an Excel file is extremely important. The conversion of CSV to Excel is made easy when using Python Pandas. Pandas can convert any file format, from CSV to PDF to Excel format, with ease. Python uses ExcelWriter() and excel() functions to convert CSV to an Excel file.

On this page, we have listed the best ways to convert CSV to Excel, with the help of which one can easily start converting the files. Scroll down to find more.

How Do I Convert a CSV File to Excel Using Python?

Before learning how to save CSV files as XLSV, let us understand what a CSV file is.

CSV: A CSV file is a comma-separated values file that allows you to save data in a tabular manner. CSV files resemble standard spreadsheets but have a.csv extension.

CSV files can be opened in virtually any spreadsheet tool, including Microsoft Excel and Google Spreadsheets. They differ from other spreadsheet file types in that they can only have one sheet in a file and cannot save cell, column, or row data. In addition, formulas cannot be saved in this format.

Given a CSV file, the task is to convert the given CSV file to excel using Pandas in Python.

Python Program to Convert CSV to Excel using Pandas

Below are the ways to Convert the Given CSV to Excel using Pandas in python:

Sample CSV File Data:

sample csv data image for reference

 

Method #1: Using ExcelWriter() function (Without Index)

Approach:

  • Import the pandas module using the import keyword.
  • Read the existing csv file using the read_csv() function of pandas module and store it in a variable.
  • Create a new excel file using the ExcelWriter() function of pandas module and store it in a variable.
  • Pass the above created Excel file to the to_excel() function and apply it to the above CSV file.
  • Save the Excel file using the save() function.
  • The Exit of the Program.

Below is the Implementation:

#Import the pandas module using the import keyword
import pandas as pd
#Read the existing csv file using the read_csv() function of pandas module and store it in a variable.
new_dataFrame = pd.read_csv('sampleData.csv')
#Create a new excel file using the ExcelWriter() function of pandas module and store it in a variable.
new_excel = pd.ExcelWriter('SampleExcelFile.xlsx')
#Pass the above created Excel file to the to_excel() function and apply it to the above CSV file
new_dataFrame.to_excel(new_excel, index=False)
#ave the Excel file using the save() function.
new_excel.save()

Output:

csv to excel converted file output sample without index

Method #2: Using ExcelWriter() function (With Index)

Approach:

  • Import the pandas module using the import keyword.
  • Read the existing csv file using the read_csv() function of pandas module and store it in a variable.
  • Create a new excel file using the ExcelWriter() function of pandas module and store it in a variable.
  • Pass the above created Excel file to the to_excel() function and apply it to the above CSV file and set the index to True.
  • Save the Excel file using the save() function.
  • The Exit of the Program.

Below is the Implementation:

#Import the pandas module using the import keyword
import pandas as pd

#Read the existing csv file using the read_csv() function of pandas module and store it in a variable.
new_dataFrame = pd.read_csv('sampleData.csv')
#Create a new excel file using the ExcelWriter() function of pandas module and store it in a variable.
new_excel = pd.ExcelWriter('SampleExcelFile.xlsx')
#Pass the above created Excel file to the to_excel() function and apply it to the above CSV file and set the index to True.
new_dataFrame.to_excel(new_excel, index=False)
#ave the Excel file using the save() function.
new_excel.save()

Output:

csv to excel converted file output sample with index

Method #3: Using to_excel() Function (Without Index)

The read_* functions read data into pandas, while the to_* methods save data. The data is saved as an excel file using the to excel() method. The row index labels are not stored in the spreadsheet when index=False is set.

Approach:

  • Import the pandas module using the import keyword
  • Read the existing CSV file using the read_csv() function of the pandas module and store it in a variable.
  • Pass the excel file path, sheet name, and index value as arguments to the to_excel to convert the given CSV to an excel file with the given sheet name.
  • The Exit of the Program.

Below is the Implementation:

#Import the pandas module using the import keyword
import pandas as pd
#Read the existing CSV file using the read_csv() function of the pandas module and store it in a variable.
new_dataFrame = pd.read_csv('sampleData.csv')

# Pass the excel file path, sheet name, and index value as arguments to the to_excel
# to convert the given CSV to an excel file with the given sheet name.
new_dataFrame.to_excel("SampleExcelFile.xlsx", sheet_name="Subjects", index=False)

Output:

csv to excel converted file output sample without index

Method #4: Using to_excel() Function (With Index)

Approach:

  • Import the pandas module using the import keyword
  • Read the existing CSV file using the read_csv() function of the pandas module and store it in a variable.
  • Pass the excel file path, sheet name, and index value as True as arguments to the to_excel to convert the given CSV to an excel file with the given sheet name.
  • The Exit of the Program.

Below is the Implementation:

#Import the pandas module using the import keyword
import pandas as pd
#Read the existing CSV file using the read_csv() function of the pandas module and store it in a variable.
new_dataFrame = pd.read_csv('sampleData.csv')
# Pass the excel file path, sheet name, and index value as True as arguments to the to_excel
# to convert the given CSV to an excel file with the given sheet name.
new_dataFrame.to_excel("SampleExcelFile.xlsx", sheet_name="Subjects", index=False)

Output:

csv to excel converted file output sample with index

Excel is one of the most powerful spreadsheets that help users work with large datasets, so conversions of various file formats into Excel files are extremely important. Using the ExcelWriter class, one can easily convert various types of files to the Excel file format.

Leave a Comment