6.10. Numpy Pandas quiz

import pandas as pd
import numpy as np

For the duration of this quiz, assume that pandas has been imported as pd and numpy as np, as in the cell above.

names2000 = pd.read_csv('names/yob2000.txt',names=['name','sex','births'])

Next assume that names2000 is the result of the above read command.

6.10.1. Selecting columns and rows

In the next cell, write down what type of Python object names2000 is after the cell above has been executed:


In the next cell, write an expression selecting the sex column of names2000:


In the next cell wrte an expression that retrieves the fourth through the sixth row of the birth column of names2000 (keeping in mind that the second row is indexed 1):


6.10.2. Selecting multiple columns

What if we just want to know the names and the birth counts, but not the gender? Pandas makes it really easy to select a subset of the columns. Write an expression that returns the subtable of the names2000 dataframe that contains just the names and the births columns:


When you executed the expression that showed you the subtable, it just showed you a summary. Write an expression that just returns the first 18 rows of the subtable:


6.10.3. Numpy

Assume the following code has been executed:

import numpy as np
x = np.array([4,3,1,0])
y =np.arange(5)
z = 2 * x

Write expressions in the next cell to retrieve 0 from x, 4 from y, and 6 from z:


In the next cell, write an expression that generates a 3 by 4 array filled with zeros, and another that generates a 3 by 1 array filled with ones:


In the next cell, write an expression that uses an assignment to a splice to make all the even values in a be 1. Attention: This can be done more easily in numpy than it can in normal Python. See if you can do it the easy way:

[ ]: a = np.arange(1,5)

In the next cell write an expression that produces an array containing result of adding 3 to each of the first 5 integers (1 - 5). There’s a hard way to do this and an easy way. The easy way uses elementwise operations: