{
"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 pandas Series `my_series`, generate a NumPy array that contains only the unique values from `my_series`. Assign this new array to a variable called `my_array`. Print `my_array` to ensure that the operation has been executed successfully."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 1\n",
"1 1\n",
"2 2\n",
"3 2\n",
"4 3\n",
"5 3\n",
"6 4\n",
"7 4\n",
"8 5\n",
"9 5\n",
"10 6\n",
"11 6\n",
"12 7\n",
"13 7\n",
"14 8\n",
"15 8\n",
"16 9\n",
"17 9\n",
"dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_series = pd.Series([1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9])\n",
"my_series"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"my_array = my_series.unique()\n",
"my_array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 4\n",
"Given the pandas DataFrame `my_data_frame`, generate a NumPy array that contains only the unique values from the second column. Assign this new array to a variable called `another_array`. Print `another_array` to ensure the operation has been executed successfully."
]
},
{
"cell_type": "code",
"execution_count": 5,
"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",
" 0.950120 | \n",
" 1.104541 | \n",
" -0.135333 | \n",
" -2.157449 | \n",
" -1.786119 | \n",
"
\n",
" \n",
" 1 | \n",
" -1.772171 | \n",
" 0.207613 | \n",
" -1.480314 | \n",
" 0.191361 | \n",
" -2.296765 | \n",
"
\n",
" \n",
" 2 | \n",
" -0.576407 | \n",
" -0.615181 | \n",
" 1.233100 | \n",
" 0.092227 | \n",
" -1.881353 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 1 2 3 4\n",
"0 0.950120 1.104541 -0.135333 -2.157449 -1.786119\n",
"1 -1.772171 0.207613 -1.480314 0.191361 -2.296765\n",
"2 -0.576407 -0.615181 1.233100 0.092227 -1.881353"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_data_frame = pd.DataFrame(np.random.randn(3,5))\n",
"my_data_frame"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.95011976, -1.7721715 , -0.57640705])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"another_array = my_data_frame[0].unique()\n",
"another_array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 5\n",
"Count the occurence of every element within the `my_series` variable that was created earlier in these practice problems."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9 2\n",
"8 2\n",
"7 2\n",
"6 2\n",
"5 2\n",
"4 2\n",
"3 2\n",
"2 2\n",
"1 2\n",
"dtype: int64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_series.value_counts()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 6\n",
"Given the function `triple_digit`, apply this to every element within `my_series`."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def triple_digit(x):\n",
" return x + x*10 + x*100"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 111\n",
"1 111\n",
"2 222\n",
"3 222\n",
"4 333\n",
"5 333\n",
"6 444\n",
"7 444\n",
"8 555\n",
"9 555\n",
"10 666\n",
"11 666\n",
"12 777\n",
"13 777\n",
"14 888\n",
"15 888\n",
"16 999\n",
"17 999\n",
"dtype: int64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Solution goes here\n",
"my_series.apply(triple_digit)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 7\n",
"Sort the `my_data_frame` variable that we created earlier based on the contents of its second column."
]
},
{
"cell_type": "code",
"execution_count": 10,
"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",
" 1 | \n",
" -1.772171 | \n",
" 0.207613 | \n",
" -1.480314 | \n",
" 0.191361 | \n",
" -2.296765 | \n",
"
\n",
" \n",
" 2 | \n",
" -0.576407 | \n",
" -0.615181 | \n",
" 1.233100 | \n",
" 0.092227 | \n",
" -1.881353 | \n",
"
\n",
" \n",
" 0 | \n",
" 0.950120 | \n",
" 1.104541 | \n",
" -0.135333 | \n",
" -2.157449 | \n",
" -1.786119 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 1 2 3 4\n",
"1 -1.772171 0.207613 -1.480314 0.191361 -2.296765\n",
"2 -0.576407 -0.615181 1.233100 0.092227 -1.881353\n",
"0 0.950120 1.104541 -0.135333 -2.157449 -1.786119"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_data_frame.sort_values(0)"
]
}
],
"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
}