Skip to content

Commit

Permalink
class 2
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuwq0 committed Sep 7, 2024
1 parent f9b4b20 commit 201dba4
Show file tree
Hide file tree
Showing 10 changed files with 790 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docs/scripts/data/earthquakes.csv filter=lfs diff=lfs merge=lfs -text
docs/scripts/data/global_temperature.csv filter=lfs diff=lfs merge=lfs -text
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,13 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/


# Jupyter Notebook
docs/.DS_Store
docs/scripts/data/database.csv
docs/scripts/data/GlobalLandTemperaturesByCity.csv
docs/scripts/data/GlobalLandTemperaturesByCountry.csv
docs/scripts/data/GlobalLandTemperaturesByMajorCity.csv
docs/scripts/data/GlobalLandTemperaturesByState.csv
docs/scripts/data/GlobalTemperatures.csv
52 changes: 21 additions & 31 deletions docs/exercises/00_introduction_python101.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"metadata": {},
"outputs": [],
"source": []
},
Expand All @@ -54,11 +50,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"metadata": {},
"outputs": [],
"source": []
},
Expand All @@ -81,11 +73,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"metadata": {},
"outputs": [],
"source": []
},
Expand All @@ -105,11 +93,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"metadata": {},
"outputs": [],
"source": []
},
Expand All @@ -127,11 +111,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"metadata": {},
"outputs": [],
"source": []
},
Expand All @@ -158,18 +138,28 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
269 changes: 269 additions & 0 deletions docs/exercises/01_numpy_pandas.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NumPy and Pandas Exercises"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## NumPy Exercises\n",
"\n",
"- Create a 1D array of numbers from 0 to 9"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Create a 3x3 matrix with values ranging from 0 to 8"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Generate a random number between 0 and 1 (hint: import np.random)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Extract all odd numbers from the following array:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Replace all odd numbers in the following array with -1:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Stack two arrays vertically and horizontally:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"a = np.arange(10).reshape(2,-1)\n",
"b = np.repeat(1, 10).reshape(2,-1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pandas Exercises\n",
"\n",
"- Create a DataFrame with 3 columns (A, B, C) and 3 rows of random numbers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Set the index of the DataFrame to be ['X', 'Y', 'Z']\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Add a new column 'D' which is the sum of columns A and B"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Drop column 'C' from the DataFrame"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Create a DataFrame from the following dictionary of lists:\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],\n",
" 'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],\n",
" 'name': ['Muffy', 'Nyarko', 'Sasha', 'Aura', 'Sunny', 'Zoro', 'Sassy', 'Penny', 'Kody', 'Cooper'],\n",
" 'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],\n",
" 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- From the DataFrame above, select all rows where the animal is a cat and the age is less than 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- From the DataFrame above, calculate the mean age for each animal type"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- For the DataFrame above, sort the DataFrame first by the values in the 'age' column in descending order, then by the value in the 'visits' column in ascending order"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- For the DataFrame above, replace any NaN values in the 'age' column with the median age"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 201dba4

Please sign in to comment.