How to Read CSV or Excel Files in Pandas

This tutorial shows how you can read CSV or Excel files into Pandas in Python. To do that, you can import Pandas and then use its function of read_csv() to read csv files in Python.

The following is the Python code to read the CSV file into Python using Pandas.

# import the package of pandas into the Python environment
import pandas as pd
# using the read_csv() function from Pandas package to read the CSV file into Python environment
df=pd.read_csv("car_data.csv")
# print out the dataframe saved above
print(df)

The following shows the print out of the data table in Pandas.

   Unnamed: 0  Brand Location  Year    DateTime
0           0  Tesla       CA  2019  2019-03-10
1           1  Tesla       CA  2018  2018-03-11
2           2  Tesla       NY  2020  2020-01-01
3           3   Ford       MA  2019  2019-03-24

Further, besides CSV files, you can use Pandas function of read_excel() to read Excel files in Python. The following shows the example of how to do so.

# read Excel file into Python using function in Pandas
df=pd.read_excel("Test_sheets.xlsx")
# print out the data table saved
print(df)

The following is the table in Python, and it is originally from the Excel file.

   Unnamed: 0  Brand Location  Year
0           0  Tesla       CA  2019
1           1  Tesla       CA  2018
2           2  Tesla       NY  2020
3           3   Ford       MA  2019

Other Resources

The following includes tutorials on this site about CSV, Excel, and TXT files in Pandas and Python broadly. If interested, you can check them out to help you better understand these topics.