O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a DataFrame from a CSV file

A data frame can be created by reading data from a CSV file using the pd.read_csv() function.

pd.read_csv() will be more extensively examined in Chapter 9, Accessing Data.

To demonstrate this process, we will load data from a file that contains a snapshot of the S&P 500. This file is named sp500.csv and is located in the code bundle's data directory.

The first line of the file has the names of each variable/column, and the remaining 500 lines represent the values for the 500 different stocks.

The following code loads the data, while specifying which column in the file to use for the index and also that we only want four specific columns (0, 2, 3, and 7):

Examining the first five rows using .head() shows ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required