Reading data from CSV/EXCEL file in python

In order to read data from csv or excel files you can use pandas library. The function is read_csv() or read_excel() from pandas.

You have to provide the file path as a string. Make sure the file name and extension is also given in the path.

In the below example a file named “CarPricesData.csv” is being read in python. The path is provided along with the file name with extension as a string. The separator is provided as comma “,”. Most of the time the CSV files will have separator as comma”,” but sometime it can be a tab or a special character like tilda “~” as well. Depending on the incoming file, you should specify the separator.

The encoding = ‘latin-1’ means the character set to use. Default character set is utf-8 which works for most of the datasets, however is there are certain cases when the data was coded specifically in latin-1 characters, then utf-8 will fail, hence use encoding = ‘latin-1’ for such cases.

Sample Output:

Read csv file using pandas in python
Read csv file using pandas in python

Reading an Excel file using pandas

In order to read an excel file, the command is read_excel() from pandas library. Here also, you just need to provide the path of the file along with the file name and correct extension like .xls or xlsx depending on the type of file you have.

The argument sheet_name=’car data’ is used to specify which sheet needs to be imported from the excel.

Sample Output:

Reading an excel file using pandas in Python
Reading an excel file using pandas in Python

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

2 thoughts on “Reading data from CSV/EXCEL file in python”

Leave a Reply!

Your email address will not be published. Required fields are marked *