# import packages
import numpy as np
import pandas as pdAssignment 4
Instructions:
Assignment 4 will cover the NumPy and Pandas packages. The overall goal of this assignment is to ensure that you are comfortable with the NumPy and Pandas packages. Follow the steps below to ensure that you receive a passing grade:
- Complete the following tasks within this notebook
- When finished, convert this notebook to an HTML/PDF file
- Place the following items into a zip folder:
- assignment.HTML or assignment.PDF
- assignment.ipynb
- assignment.csv
- Name this folder using the naming convention: FIRSTNAME_LASTNAME_ASSIGNMENT4.zip
- For example, if your name was Jane Doe, then the zipped folder should be called JANE_DOE_ASSIGNMENT4.zip
1. Manipulate an Array
Without using the np.array() function, manipulate (in any way you choose) a \(2 \times 6\) array of zeros, to match the final \(4 \times 3\) array shown below.
\[start \rightarrow \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix} \rightarrow \text{manipulate} \rightarrow \begin{bmatrix} 12 & 11 & 10\\ 9 & 8 & 7\\ 10 & 10 & 10\\ 3 & 2 & 1\\ \end{bmatrix}\]
target = np.zeros(shape = (2,6))
# your code goes here
print(target)2. Create a DataFrame and Clean
A. Create a DataFrame using the pd.DataFrame() function that is identical to the table below. Remember to use np.nan to signify missing values.
| name | gender | weight_kg | height_cm |
|---|---|---|---|
| Jacinda | F | 55.51 | NaN |
| Oralla | M | 60.4 | 162.79 |
| Aeriell | F | NaN | 162.32 |
| Portie | X | 73.94 | 160.33 |
| Ferdinanda | F | 60.62 | 146.68 |
# your code goes hereB. Replace the missing value in the weight_kg column with the mean weight across the weight_kg column
# your code goes hereC. Replace the missing value in the height_cm column with the maximum of the heights across the height_cm column
# your code goes hereD. Replace the X value in the gender column with the mode of the gender column
# your code goes hereE. Print the cleaned DataFrame
# your code goes here3. DataFrame Manipulation
Let’s load in the exercise dataset from the seaborn package.
import seaborn as sns
exercise = sns.load_dataset("exercise") # this requries an internet connection
print(exercise)Perform the following tasks on the DataFrame:
A. Remove the unnamed column from the DataFrame
# your code goes hereB. The time column is a string type. Use the .str.split() function to extract the numbers out of this column. Save these numbers as a new column called minutes. What is the data type of this new column? Should it be transformed to a different data type? If so, perform the transformation.
# your code goes hereC. Since we have the minutes column, drop the original time column from the DataFrame
# your code goes hereD. Subset the DataFrame to only contain entries where the minutes column is greater than 10. Use the .min() function to determine what is the minimum pulse is for activities that lasted more than 10 minutes
# your code goes hereE. Use the groupby() function to group the kind column. Then aggregate to find the mean pulse for each kind of activity in the DataFrame
# your code goes hereF. Save the DataFrame as a CSV file named assignment.csv. Make sure that the index argument is set to False
# your code goes here