Creating a Pandas DataFrame from a CSV file

With many datasets provided in the CSV format, creating a Pandas DataFrame from a CSV file is one of the most common methods.

How to do it…

  1. To create a Pandas DataFrame from a CSV file, we begin by importing the Python libraries. For this, use the following command:
    import pandas as pd
  2. Next, we will define a variable for the accidents data file as follows:
    accidents_data_file = '/Users/robertdempsey/Dropbox/private/Python Business Intelligence Cookbook/Data/Stats19-Data1979-2004/Accidents7904.csv'
  3. Next, we create a DataFrame from the data using the following code:
    accidents = pd.read_csv(accidents_data_file, sep=',', header=0, index_col=False, parse_dates=True, tupleize_cols=False, error_bad_lines=False, warn_bad_lines=True, ...

Get Python Business Intelligence Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.