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.
1 2 3 4 5 6 7 8 9 |
# Use pandas to read a csv file by prodiving the path of file to read_csv() function # Many other formats are available e.g read_excel(), read_table(), read_json etc. # these options can be seen using dot tab option # The output of read_csv function here is stored as a DataFrame CarPricesData=pd.read_csv(filepath_or_buffer='/Users/farukh/DATA/CarPricesData.csv', sep=',', encoding='latin') # Printing few records of the data CarPricesData.head(n=5) |
Sample Output:

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.
1 2 3 4 5 6 7 |
# Use pandas to read a excel file by prodiving the path of file # The output of read_excel() function here is stored as a DataFrame CarPricesData=pd.read_excel(io='/Users/farukh/DATA/CarPricesData.xlsx', sheet_name='car data') # Printing few records of the data CarPricesData.head(n=5) |
Sample Output:

well explained
Thank you, Sundar!