How to convert Excel to CSV Python Pandas
In this article, you will learn how to convert Excel to CSV using Python Pandas.
Pandas is open source, fast, flexible, powerful, easy-to-use tools and are widely used for data manipulation and data analysis. It provides functionality to read data from various file formats, such as CSV, MS Excel, etc. As we know, Microsoft Excel has beed used in many different application and spreadsheets representation. We can easily organize a large amount of data by using this. It is also rich in features like calculation, graphing tools, pivot tables, producing graphs and charts and much more. CSV is a widely used file format that stores data in a tabular format. Most popular programming languages have tools or applications to support the CSV file format.
CSV files have some advantages over Excel files. The CSV files are faster and consume less memory space, whereas Excel consumes more memory space while importing data. That's why it's important to convert excel to CSV.
Before starting work on Python Pandas, we need to install this module. So, open your command prompt, activate your virtual environment, and install using the following command.
pip install pandas
On successful installation, it returns something like this-
Installing collected packages: pandas Successfully installed pandas-1.0.1
Suppose we have the following excel file containing school program participant data.
Reading Excel file using Python Pandas
Here, we have first imported the Pandas module and passed the excel sheet file as a parameter in read_excel() method. This method reads the data into a Pandas DataFrame. It accepts filename in the first parameter and sheet name in the second parameter. DataFrame is the key data structure of Pandas.
import pandas as pd df = pd.read_excel("school_event.xlsx")
Converting to CSV
Pandas DataFrame provides the to_csv() method to convert data from a Dataframe to CSV. If we provide the file location as an argument, then data is written to the file otherwise, CSV string is returned. This method provides many options, like we can provide a custom delimiter, specify the columns to be used, ignore the index column, and much more to the generated CSV file. Here is the complete code to convert Excel to CSV -
import pandas as pd df = pd.read_excel("school_event.xlsx") df.to_csv ("school.csv", index = None, header=True)
You can see how we can simply achieve more work from just 2-3 lines of code. This is one of the big advantages of Python Pandas.
Related ArticlesHow to read data from excel file using Python Pandas
Python program to check leap year
Django Export Model Data to CSV
Python Converting a CSV File to a MySQL Table
Write Python Pandas Dataframe to CSV
Convert JSON to CSV using Python
Generate and download a CSV file in Django
Python Pandas CSV to Dataframe
How to read data from excel file in Python
Python read JSON from URL requests
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml