Convert CSV to Excel

Tags: #python #csv #excel #pandas #file
Author: Sophia Iroegbu
Description: This notebook provides a step-by-step guide to converting CSV files to Excel spreadsheets using Python.


Import library

import pandas as pd


# Declare the absolute path to the CSV file
csv_input = ""
# Name the XSLX spreasheet needed as an output
excel_output = "covid_dataset.xlsx"


Use pandas to:

  • read the CSV file link,
  • then convert it to Excel spreadsheet file by using the to_excel function.
The sep argument helps to seperate the rows. It might be a , or a ; and also, ensure the CSV link ends with a CSV file extension.
convert_csv = pd.read_csv(csv_input, sep=",")


Save result in .xlsx file

The header=True ensures the header from CSV file is converted. This saves the .xlsx file on your root directory.
convert_csv.to_excel(excel_output, index=False, header=True)