O'Reilly logo

Practical Data Science Cookbook - Second Edition by Abhijit Dasgupta, Benjamin Bengfort, Sean Patrick Murphy, Tony Ojeda, Prabhanjan Tattar

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

How to do it...

Let's use the following sequence of steps to import the data and start our exploration of this dataset in Python:

  1. With the following snippet, we will create a Python list in memory that contains dictionaries of each row, where the keys are the column names (the first row of the CSV contains the header information) and the values are the values for that particular row:
In [3]: import csv    ...: data_file = "../data/income_dist.csv"    ...: with open(data_file, 'r') as csvfile:    ...: reader = csv.DictReader(csvfile)    ...: data = list(reader) 
Note that the input file, income_dist.csv, might be in a different directory in your system depending on where you place it.
  1. We perform a quick check with len to reveal the number of records: ...

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