Using pandas to import flat files as DataFrames (2)
In the last exercise, you were able to import flat files
into a pandas DataFrame. As a bonus, it is then straightforward
to retrieve the corresponding
numpy array using the attribute values. You'll now have a chance
to do this using the MNIST dataset, which is available as digits.csv.
This exercise is part of the course
Importing Data in Python
Exercise instructions
- Import the first 5 rows of the file into a DataFrame using the function
pd.read_csv()and assign the result todata. You'll need to use the argumentsnrowsandheader(there is no header in this file). - Build a numpy array from the resulting DataFrame in
dataand assign todata_array. - Execute
print(type(data_array))to print the datatype ofdata_array.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Assign the filename: file
file = 'digits.csv'
# Read the first 5 rows of the file into a DataFrame: data
# Build a numpy array from the DataFrame: data_array
# Print the datatype of data_array to the shell
print(type(data_array))