Skip to content
Advertisement

Selecting columns from CSV file

Python newbie here.

I have a bunch of CSV files of stock prices. They are structured as date, opening, high, low, close, adj. close, volume. I only need the date (column 1), the opening (column 2) and close (column 5).

The task prohibits the use of pandas. How can I extract those three columns and return them?

This is my attempt, but that doesn’t extract the 3 columns I need.

JavaScript

Advertisement

Answer

Hi👋🏻 Hope you are doing well!

If I understood your question correctly, you can do something like that:

JavaScript

the main idea is: csv.DictReader returns list of dicts so you can select necessary columns by key. 🙂

Advertisement