QMB3302 EXAM 1 QUESTIONS WITH COMPLETE
SOLUTIONS
Imagine we have a dataframe, df. What would be the purpose for running code like the
one below? (why would we run it?)
df.loc[1] -- Answer ✔✔ To look for, retrieve a value from df
Imagine we have a pandas dataframe we have named 'df'.
The dataframe consists of 2 columns.
"col1" is 30 values long, and is a random mix of the letters 'a', 'b', and 'c'.
"num1" is also 30 values long, and is a random set of numerical data (all integers).
Which of the following would give you the mean of the numerical (num1) column,
grouped by the values from column "col1"? -- Answer ✔✔ df.groupby('col1'). mean()
What is the shape of the following numPy array?
,np.random.seed(1955)x = np.random.randn(2, 2, 2, 2)
print(x.shape)
x
#Hint: I have not loaded the necessary package here...but you should(load pandas,
import numpy) -- Answer ✔✔ (2, 2, 2, 2)
What is the output of the following code?
import numpy as np
list1 = [5, 5, 5]
list2 = [10, 10, 10]
np_list1 = np.array(list1)
np_list2 = np.array(list2)
np_list1/np_list2 -- Answer ✔✔ array([0.5, 0.5 0.5])
numPy allows us to do more complicated math on lists and other data structures, and is
used in most of the more advanced modules we will use (such as pandas) -- Answer ✔✔
true
pandas can be imported as import pandas as pd -- Answer ✔✔ true
Usually a programmer will use conventional names when importing packages. But it is
not strictly necessary. numpy for example can be imported as: import numpy as
humpty_dumpty -- Answer ✔✔ true
, pandas allows us to use multiple different data types (like objects and numbers) in a
single table. -- Answer ✔✔ true
for pandas to work, data must be formatted as lists before it is imported -- Answer ✔✔
false
When importing data from a local drive, the relative path was defined as the path FROM
where your code in your current working directory is, TO where your data is. --
Answer ✔✔ true
Look at the below code carefully. It is not at all uncommon to see errors of omission in
code chunks like this. How can you fix the below so that it produces the output
'array([50, 50, 100])'
import numpy as np
list1 = [5,5,5]
list2 = [10,10,20]
np_list1 = np.array(list1)
np_list2 = np.array(list2)
np_list1 is_and np_list2 -- Answer ✔✔ change "np_list1 is_and np_list2" to
"np_list1*np_list2"
pandas has functionality to work with complicated dates. -- Answer ✔✔ true
SOLUTIONS
Imagine we have a dataframe, df. What would be the purpose for running code like the
one below? (why would we run it?)
df.loc[1] -- Answer ✔✔ To look for, retrieve a value from df
Imagine we have a pandas dataframe we have named 'df'.
The dataframe consists of 2 columns.
"col1" is 30 values long, and is a random mix of the letters 'a', 'b', and 'c'.
"num1" is also 30 values long, and is a random set of numerical data (all integers).
Which of the following would give you the mean of the numerical (num1) column,
grouped by the values from column "col1"? -- Answer ✔✔ df.groupby('col1'). mean()
What is the shape of the following numPy array?
,np.random.seed(1955)x = np.random.randn(2, 2, 2, 2)
print(x.shape)
x
#Hint: I have not loaded the necessary package here...but you should(load pandas,
import numpy) -- Answer ✔✔ (2, 2, 2, 2)
What is the output of the following code?
import numpy as np
list1 = [5, 5, 5]
list2 = [10, 10, 10]
np_list1 = np.array(list1)
np_list2 = np.array(list2)
np_list1/np_list2 -- Answer ✔✔ array([0.5, 0.5 0.5])
numPy allows us to do more complicated math on lists and other data structures, and is
used in most of the more advanced modules we will use (such as pandas) -- Answer ✔✔
true
pandas can be imported as import pandas as pd -- Answer ✔✔ true
Usually a programmer will use conventional names when importing packages. But it is
not strictly necessary. numpy for example can be imported as: import numpy as
humpty_dumpty -- Answer ✔✔ true
, pandas allows us to use multiple different data types (like objects and numbers) in a
single table. -- Answer ✔✔ true
for pandas to work, data must be formatted as lists before it is imported -- Answer ✔✔
false
When importing data from a local drive, the relative path was defined as the path FROM
where your code in your current working directory is, TO where your data is. --
Answer ✔✔ true
Look at the below code carefully. It is not at all uncommon to see errors of omission in
code chunks like this. How can you fix the below so that it produces the output
'array([50, 50, 100])'
import numpy as np
list1 = [5,5,5]
list2 = [10,10,20]
np_list1 = np.array(list1)
np_list2 = np.array(list2)
np_list1 is_and np_list2 -- Answer ✔✔ change "np_list1 is_and np_list2" to
"np_list1*np_list2"
pandas has functionality to work with complicated dates. -- Answer ✔✔ true