Load a CSV file in Pandas

Python

Pandas

CSV

Load file

Papa D.

Python

Pandas

CSV

The first step for doing data science in Pandas is loading data into a DataFrame. While data can be saved on disked in many formats, comma-separated value (CSV) files are a common choice, and they are well supported by Pandas.

Code Example

Use pd.read_csv() to load a CSV file.

import pandas as pd

# Load the csv file
deniro_movies = pd.read_csv('deniro.csv')
deniro_movies.head()
Year Score Title
0 1968 86 Greetings
1 1970 17 Bloody Mama
2 1970 73 Hi, Mom!
3 1971 40 Born to Win
4 1973 98 Mean Streets

deniro.csv is a CSV file that contains the following:

"Year","Score","Title"
1968,86,"Greetings"
1970,17,"Bloody Mama"
1970,73,"Hi, Mom!"
1971,40,"Born to Win"
1973,98,"Mean Streets"
1973,88,"Bang the Drum Slowly"
1974,97,"The Godfather, Part II"
1976,41,"The Last Tycoon"
1976,99,"Taxi Driver"