{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 1\n",
"Import NumPy under the alias `np`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 2\n",
"Import pandas under the alias `pd`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 3\n",
"Given the following NumPy array `data`, create a pandas DataFrame named `first_data_frame` that contains the same elements. Print the DataFrame to make sure the operation executed successfully."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"data = np.round(np.random.randn(5,5),1)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"#Solution goes here\n",
"first_data_frame = pd.DataFrame(data)\n",
"first_data_frame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 4\n",
"Assign the values of `row_labels` to the index of `first_data_frame`. Print the DataFrame to make sure the operation executed successfully.\n",
"\n",
"Hint: It will be easier to overwrite `first_data_frame` by using another `pd.DataFrame` method."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"row_labels = ['one','two','three','four','five']"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" 1 | \n",
" 2 | \n",
" 3 | \n",
" 4 | \n",
"
\n",
" \n",
" \n",
" \n",
" one | \n",
" -1.9 | \n",
" 1.8 | \n",
" 0.6 | \n",
" 0.0 | \n",
" 1.1 | \n",
"
\n",
" \n",
" two | \n",
" 2.3 | \n",
" -2.1 | \n",
" -0.3 | \n",
" -1.8 | \n",
" -0.6 | \n",
"
\n",
" \n",
" three | \n",
" 0.7 | \n",
" 0.4 | \n",
" -1.0 | \n",
" -0.7 | \n",
" 0.3 | \n",
"
\n",
" \n",
" four | \n",
" 0.8 | \n",
" 0.5 | \n",
" -0.7 | \n",
" 0.0 | \n",
" 0.2 | \n",
"
\n",
" \n",
" five | \n",
" 1.7 | \n",
" 1.0 | \n",
" -1.3 | \n",
" 1.8 | \n",
" 1.3 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 1 2 3 4\n",
"one -1.9 1.8 0.6 0.0 1.1\n",
"two 2.3 -2.1 -0.3 -1.8 -0.6\n",
"three 0.7 0.4 -1.0 -0.7 0.3\n",
"four 0.8 0.5 -0.7 0.0 0.2\n",
"five 1.7 1.0 -1.3 1.8 1.3"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"first_data_frame = pd.DataFrame(data,row_labels)\n",
"first_data_frame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 5\n",
"Assign the values of `column_labels` to the columns of `first_data_frame`. Note that there are two main ways to do this - you are free to chose the method of your choice. Print the DataFrame to make sure the operation executed successfully."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"column_labels = ['alpha','beta','charlie','delta','echo']"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" alpha | \n",
" beta | \n",
" charlie | \n",
" delta | \n",
" echo | \n",
"
\n",
" \n",
" \n",
" \n",
" one | \n",
" -1.9 | \n",
" 1.8 | \n",
" 0.6 | \n",
" 0.0 | \n",
" 1.1 | \n",
"
\n",
" \n",
" two | \n",
" 2.3 | \n",
" -2.1 | \n",
" -0.3 | \n",
" -1.8 | \n",
" -0.6 | \n",
"
\n",
" \n",
" three | \n",
" 0.7 | \n",
" 0.4 | \n",
" -1.0 | \n",
" -0.7 | \n",
" 0.3 | \n",
"
\n",
" \n",
" four | \n",
" 0.8 | \n",
" 0.5 | \n",
" -0.7 | \n",
" 0.0 | \n",
" 0.2 | \n",
"
\n",
" \n",
" five | \n",
" 1.7 | \n",
" 1.0 | \n",
" -1.3 | \n",
" 1.8 | \n",
" 1.3 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" alpha beta charlie delta echo\n",
"one -1.9 1.8 0.6 0.0 1.1\n",
"two 2.3 -2.1 -0.3 -1.8 -0.6\n",
"three 0.7 0.4 -1.0 -0.7 0.3\n",
"four 0.8 0.5 -0.7 0.0 0.2\n",
"five 1.7 1.0 -1.3 1.8 1.3"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"first_data_frame.columns = column_labels\n",
"first_data_frame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 6\n",
"Create a pandas Series named `my_series` that contains the values from row `alpha` of `first_data_frame`. Print `my_series` to make sure the operation executed successfully."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"one -1.9\n",
"two 2.3\n",
"three 0.7\n",
"four 0.8\n",
"five 1.7\n",
"Name: alpha, dtype: float64"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"my_series = first_data_frame['alpha']\n",
"my_series"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 7\n",
"Create a new DataFrame called `second_data_frame` that is equal to `first_data_frame` but without row `one`. Print `second_data_frame` to make sure the operation executed successfully."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" alpha | \n",
" beta | \n",
" charlie | \n",
" delta | \n",
" echo | \n",
"
\n",
" \n",
" \n",
" \n",
" two | \n",
" 2.3 | \n",
" -2.1 | \n",
" -0.3 | \n",
" -1.8 | \n",
" -0.6 | \n",
"
\n",
" \n",
" three | \n",
" 0.7 | \n",
" 0.4 | \n",
" -1.0 | \n",
" -0.7 | \n",
" 0.3 | \n",
"
\n",
" \n",
" four | \n",
" 0.8 | \n",
" 0.5 | \n",
" -0.7 | \n",
" 0.0 | \n",
" 0.2 | \n",
"
\n",
" \n",
" five | \n",
" 1.7 | \n",
" 1.0 | \n",
" -1.3 | \n",
" 1.8 | \n",
" 1.3 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" alpha beta charlie delta echo\n",
"two 2.3 -2.1 -0.3 -1.8 -0.6\n",
"three 0.7 0.4 -1.0 -0.7 0.3\n",
"four 0.8 0.5 -0.7 0.0 0.2\n",
"five 1.7 1.0 -1.3 1.8 1.3"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"second_data_frame = first_data_frame.drop('one')\n",
"second_data_frame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 8\n",
"Create a new DataFrame called `third_data_frame` that is equal to `second_data_frame`, but without row `charlie`. Print `third_data_frame` to make sure the operation executed successfully."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" alpha | \n",
" beta | \n",
" delta | \n",
" echo | \n",
"
\n",
" \n",
" \n",
" \n",
" two | \n",
" 2.3 | \n",
" -2.1 | \n",
" -1.8 | \n",
" -0.6 | \n",
"
\n",
" \n",
" three | \n",
" 0.7 | \n",
" 0.4 | \n",
" -0.7 | \n",
" 0.3 | \n",
"
\n",
" \n",
" four | \n",
" 0.8 | \n",
" 0.5 | \n",
" 0.0 | \n",
" 0.2 | \n",
"
\n",
" \n",
" five | \n",
" 1.7 | \n",
" 1.0 | \n",
" 1.8 | \n",
" 1.3 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" alpha beta delta echo\n",
"two 2.3 -2.1 -1.8 -0.6\n",
"three 0.7 0.4 -0.7 0.3\n",
"four 0.8 0.5 0.0 0.2\n",
"five 1.7 1.0 1.8 1.3"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"third_data_frame = second_data_frame.drop('charlie', axis=1)\n",
"third_data_frame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 9\n",
"Create a variable called `row_two` that is equal to row `two` from `third_data_frame`. Print `row_two` to make sure the operation executed successfully."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"alpha 2.3\n",
"beta -2.1\n",
"delta -1.8\n",
"echo -0.6\n",
"Name: two, dtype: float64"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"row_two = third_data_frame.loc['two']\n",
"row_two"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 10\n",
"Print the shape of new_data."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"new_data = np.round(np.random.randn(5,5),1)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5, 5)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"new_data.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 11\n",
"Print a DataFrame that contains boolean values that indicate whether the elements of `new_data` are greater than 1."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" 1 | \n",
" 2 | \n",
" 3 | \n",
" 4 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" True | \n",
" True | \n",
" True | \n",
" False | \n",
" False | \n",
"
\n",
" \n",
" 1 | \n",
" False | \n",
" False | \n",
" False | \n",
" False | \n",
" True | \n",
"
\n",
" \n",
" 2 | \n",
" False | \n",
" False | \n",
" False | \n",
" True | \n",
" False | \n",
"
\n",
" \n",
" 3 | \n",
" True | \n",
" False | \n",
" False | \n",
" False | \n",
" False | \n",
"
\n",
" \n",
" 4 | \n",
" False | \n",
" False | \n",
" False | \n",
" True | \n",
" False | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 1 2 3 4\n",
"0 True True True False False\n",
"1 False False False False True\n",
"2 False False False True False\n",
"3 True False False False False\n",
"4 False False False True False"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame(new_data > 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 12\n",
"Print a NumPy array that contains only the elements of `new_data` that are greater than 1."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2.3, 1.5, 1.6, 1.2, 2.2, 1.1, 1.7])"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_data[new_data > 1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}