Thursday, December 26, 2024
Google search engine
HomeLanguagesConvert CSV to Excel using Pandas in Python

Convert CSV to Excel using Pandas in Python

Pandas can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel. In this article, we will be dealing with the conversion of .csv file into excel (.xlsx). 
Pandas provide the ExcelWriter class for writing data frame objects to excel sheets. 
Syntax: 
 

final = pd.ExcelWriter('GFG.xlsx')

Example:
Sample CSV File:
 

python-csv-to-json

 

Python3




import pandas as pd
 
 
# Reading the csv file
df_new = pd.read_csv('Names.csv')
 
# saving xlsx file
GFG = pd.ExcelWriter('Names.xlsx')
df_new.to_excel(GFG, index=False)
 
GFG.save()


Output:
 

python-csv-to-excel

 Method 2:

The read_* functions are used to read data to pandas, the to_* methods are used to store data. The to_excel() method stores the data as an excel file. In the example here, the sheet_name is named passengers instead of the default Sheet1. By setting index=False the row index labels are not saved in the spreadsheet.

Python3




import pandas as pd
 
# The read_csv is reading the csv file into Dataframe
 
df = pd.read_csv("./weather_data.csv")
 
# then to_excel method converting the .csv file to .xlsx file.
 
df.to_excel("weather.xlsx", sheet_name="Testing", index=False)
 
# This will make a new "weather.xlsx" file in your working directory.
# This code is contributed by Vidit Varshney


Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments