With many datasets provided in the CSV format, creating a Pandas DataFrame from a CSV file is one of the most common methods.
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
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'
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, skip_blank_lines=True )
Show the first five rows of the DataFrame using the
head()
function. By default,head()
returns the first five rows:accidents.head()
We saw a version of this recipe earlier, in Chapter 2, Making...