Enable Dark Mode!
how-to-convert-csv-to-excel-using-pandas-in-python.jpg
By: Rosmy John

How to Convert CSV to Excel using Pandas in Python

Technical

The CSV means comma-separated values. It is a file that uses commas to separate values. Each line represents the data record. CSV is a frequent facts alternate layout that is extensively supported through consumer, business, and scientific applications. Most database applications can export statistics as CSV, and the exported CSV file can then be imported by using the spreadsheet program. In python, we convert CSV files to excel by using pandas. Pandas is a python package supplying fast, quick, and bendy statistics buildings designed to make working with “relational” or “labeled” information handy and intuitive. For the conversion of CSV to Excel, there are some procedures. 

Step 1: Install the Pandas package

Install Pandas Package. We can use the following command to deploy the pandas package.
pip install pandas

Step 2: Give the path where the CSV file is stored

Next, we have to capture the path of the CSV file that is stored on our computer. Here is the example of the path where a CSV file is stored, for example,
/home/cybrosys/Documents/Product_List.csv
Product_list is the file name, and CSV is the file format

Step 3: Specify the path where the Excel file is to be stored

Next, We need to specify the path where the Excel file is to be stored for example
/home/cybrosys/Documents/New_Products.xlsx

Step 4: Convert CSV to Excel using Python

In the final step we should need to use the following template to perform this action
import pandas as pd
read_file_product = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
read_file_product.to_excel (r'Path to store the Excel file\File name.xlsx', index = None, header=True)

For Example:

import pandas as pd
read_file = pd.read_csv (r'/home/cybrosys/Documents/Product_List.csv')
read_file.to_excel (r'/home/cybrosys/Documents/New_Products.xlsx', index = None, header=True)
CSV documents can be opened in actually any spreadsheet tool, inclusive of Microsoft Excel and Google Spreadsheets. They range from different spreadsheet file sorts in that they can solely have one sheet in a file and can't keep cell, column, or row data. In addition, the formulation can't be saved in this format. 
There are different methods to convert CSV to Excel files.

Method: 1 – Using ExcelWriter() without index

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

Example:

import pandas as pd
new_dataFrame = pd.read_csv('sample.csv')
new_excel = pd.ExcelWriter('SampleFile.xlsx')
new_dataFrame.to_excel(new_excel, index=False)
new_excel.save()

Method: 2 – Using ExcelWriter() with index

a) Import the pandas module.
b) Read the present CSV file with the use of the read_csv() characteristic of the pandas module and keep it in a variable.
c) Create a new excel file with the use of the ExcelWriter() characteristic of the pandas module and shop it in a variable.
d) Pass the above created Excel file to the to_excel() characteristic, follow it to the above CSV file, and set the index to True.
e) Save the Excel file.
f) Exit the Program.

Example:

import pandas as pd
new_dataFrame = pd.read_csv('sample.csv')
new_excel = pd.ExcelWriter('SampleFile.xlsx')
new_dataFrame.to_excel(new_excel, index=False)
new_excel.save()

Method: 3 – Using to_excel() 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.
a) Import the pandas
b) Read the present CSV file with the use of the read_csv() characteristic of pandas module and keep it in a variable.
d) Should Pass the excel file path, sheet name, and index fee as arguments to the to_excel to convert the given CSV to an excel file with the given sheet name.
e) Exit the Program.

Example:

import pandas as pd
new_dataFrame = pd.read_csv('sample.csv')
new_dataFrame.to_excel("SampleFile.xlsx", sheet_name="Subjects", index=False)

Method: 4 – Using to_excel() with index

a) Import the pandas module.
b) Read the present CSV file using the read_csv() characteristic of the pandas module and keep it in a variable.
c) Pass the excel file path, sheet name, and index fee as True as arguments to the to_excel to convert the given CSV to an excel file with the given sheet name.
d) Exit the Program.

Example:

import pandas as pd
new_dataFrame = pd.read_csv('sampleData.csv')
new_dataFrame.to_excel("SampleExcelFile.xlsx", sheet_name="Subjects", index=False)
If you have a dataset in CSV layout and you desire to operate some mathematical operations, then changing them to an Excel file is extraordinarily important. The conversion of CSV to Excel is made convenient when using Python Pandas. Pandas can easily convert any file format, from CSV to PDF to Excel format, Python makes use of ExcelWriter(), and excel() features to convert CSV to an Excel file.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message