Quiz: Python Data Import and Analysis


1. What are the two common Python packages for importing and analyzing data that are reviewed here? Select two options.

A. CSV reader
Incorrect. The csv package does have a reader function, but it is not reviewed here.
B. Numpy
Correct. Numpy has loadtxt as a function to import data.
C. Scipy
Incorrect. Scipy (or Scientific Python) is a common package for analyzing data, but is not commonly known for data import functions.
D. Pandas
Correct. Pandas can import many types of files into DataFrames that are tables of the imported data.
E. Excel
Incorrect. Excel can import data but it is not a Python function.

2. Slicing a data set in Python means that you take a part of the larger data set. In order to print the first 5 rows of a pandas data set called data, what command would you use?

A. print(data[1:5])
Incorrect. This command prints rows 1,2,3,4 (4 total rows). Python starts with a zero index. The last number (5) is not included.
B. print(data[0:5])
Correct. This command prints rows 0,1,2,3,4 (5 total rows). Python starts with a zero index. The last number (5) is not included.
C. print(data[0:6])
Incorrect. This command prints rows 0,1,2,3,4,5 (6 total rows).

3. What command would you use to print the last 5 rows of data as a pandas object?

A. print(data[-5:])
Correct. data[-1] is the last row, data[-2] is the second to last row, and so on. The missing number after the : symbol indicates the end.
B. print(data[-5])
Incorrect. This command only prints the 5th to last row of data.
C. print(data[-6])
Incorrect. This command only prints the 6th to last row of data.
D. print(data[-4:])
Incorrect. data[-1] is the last row, data[-2] is the second to last row, and so on. The missing number after the : symbol indicates the end. This command prints the 4th to last to the last row and misses the 5th to last row.

4. Suppose that you want to export the pandas object "data" from Python. Which commands are valid? Select all that are correct. See Data Science: Gather Data for a list of export functions.

A. data.to_csv('result.csv') # to export to a CSV file
Correct.
B. data.to_excel('result.xlsx') # to export to an Excel file
Correct.
C. data.to_clipboard()
Correct.

Home | Quiz: Python Data Import and Analysis