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 tool and most 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 widespread used in many different application spreadsheet representation. We can easily organize large amount of data by using this. It has also rich in features like calculation, graphing tools, pivot tables, produce graphs and charts and much more. CSV is a widely used file format that stores data in a tabular format. All most all popular programming languages has tools or applications to support the CSV file format.
CSV files have some advantages over the Excel files. The CSV files are faster and consumes less memory space, whereas the Excel consumes more memory space while importing data. That's why it serves importance 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 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 to_csv() method to convert data from a Dataframe to CSV. If we will provide the file location as 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
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