Customizing your NumPy import
What if there are rows, such as a header, that we wish to not import? What if our file has a delimiter other than a comma? What if you only wish to import particular columns?
There are a number of arguments that np.loadtxt() takes
that you'll find useful: delimiter changes the delimiter
that loadtxt() is expecting, for example, ',' and '\t' for comma-delimited and
tab-delimited respectively; skiprows allows you to specify
how many rows you wish to skip; usecols takes a list of
the indices of the columns you wish to keep.
The file that you'll be importing, digits_header.txt
- has a header;
- is tab-limited.
This exercise is part of the course
Importing Data in Python
Exercise instructions
- Complete the arguments of
np.loadtxt(): the file you're importing is tab-delimited, you want to skip the first row and you only want to import the first and third columns. - Complete the argument of the
print()call in order to print the entire array that you just imported.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import numpy
import numpy as np
# Assign the filename: file
file = 'digits_header.txt'
# Load the data: data
data = np.loadtxt(____, ____, skiprows=____, usecols=____)
# Print data
print(____)