Skip to content

Commit

Permalink
Final QA (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
gahjelle authored Oct 26, 2024
1 parent 565dfd1 commit 2487b88
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
20 changes: 12 additions & 8 deletions pandas-reset-index/Main_Code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"metadata": {},
"outputs": [],
"source": [
"!Python -m pip install pandas pyarrow"
"!python -m pip install pandas pyarrow"
]
},
{
Expand Down Expand Up @@ -381,9 +381,11 @@
"id": "a822bb24-af5b-47d1-b51e-0d93a419e95f",
"metadata": {},
"source": [
"# This code will fail due to the duplicate index.\n",
"> The following code will fail due to the duplicate index.\n",
"\n",
"all_beach_boys.loc[3:4]"
"```\n",
"all_beach_boys.loc[3:4]\n",
"```"
]
},
{
Expand Down Expand Up @@ -411,9 +413,11 @@
"id": "cf30ce38-8b34-426b-801d-4329a19b36e8",
"metadata": {},
"source": [
"# This code will fail due to the duplicate index.\n",
"> The following code will fail due to the duplicate index:\n",
"\n",
"all_beach_boys.filter(items=[1, 3], axis=\"index\")"
"```\n",
"all_beach_boys.filter(items=[1, 3], axis=\"index\")\n",
"```"
]
},
{
Expand Down Expand Up @@ -445,7 +449,7 @@
"metadata": {},
"outputs": [],
"source": [
"beach_boys.index = [f\"Employee_{x}\" for x in range(len(beach_boys))]\n",
"beach_boys.index = [f\"Employee_{x + 1}\" for x in range(len(beach_boys))]\n",
"beach_boys"
]
},
Expand All @@ -456,7 +460,7 @@
"metadata": {},
"outputs": [],
"source": [
"beach_boys.loc[[\"Employee_3\"]]"
"beach_boys.loc[[\"Employee_4\"]]"
]
},
{
Expand Down Expand Up @@ -631,7 +635,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.0"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion pandas-reset-index/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# How to Reset a pandas DataFrame Index

These are the download files you can use with the Real Python tutorial [How to Reset a pandas DataFrame Index](https://realpython.com/how-to-pandas-reset-index-dataframe/):
These are the download files you can use with the Real Python tutorial [How to Reset a pandas DataFrame Index](https://realpython.com/pandas-reset-index/):

- `Main_Code.ipynb` contains the main code from the tutorial.
- `Solutions.ipynb` contains sample solutions to the various exercises.
Expand Down
37 changes: 27 additions & 10 deletions pandas-reset-index/Solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@
"\n",
"import pandas as pd\n",
"\n",
"beach_boys = pd.read_csv(\n",
" \"band_members.csv\",\n",
" parse_dates=[\"date_of_birth\"],\n",
" dayfirst=True,\n",
").convert_dtypes(dtype_backend=\"pyarrow\")\n",
"\n",
"beach_boys = beach_boys.assign(\n",
" date_of_birth=beach_boys[\"date_of_birth\"].dt.date\n",
"beach_boys = (\n",
" pd.read_csv(\n",
" \"band_members.csv\",\n",
" parse_dates=[\"date_of_birth\"],\n",
" dayfirst=True,\n",
" )\n",
" .convert_dtypes(dtype_backend=\"pyarrow\")\n",
" .assign(\n",
" date_of_birth=lambda beach_boys: beach_boys[\"date_of_birth\"].dt.date\n",
" )\n",
")\n",
"beach_boys"
]
Expand Down Expand Up @@ -190,7 +192,7 @@
"outputs": [],
"source": [
"# (ii) Using .index\n",
"beach_boys.index = [x for x in range(len(beach_boys))]\n",
"beach_boys.index = range(len(beach_boys))\n",
"beach_boys"
]
},
Expand Down Expand Up @@ -274,13 +276,28 @@
").convert_dtypes(dtype_backend=\"pyarrow\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0de567e-5d9b-4852-af9d-d8cff07f4f5b",
"metadata": {},
"outputs": [],
"source": [
"beach_boys.index = beach_boys[\"last_name\"].str.cat(\n",
" beach_boys[\"first_name\"].str[0]\n",
")\n",
"\n",
"beach_boys"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f62038bd-fa3e-4c51-bf62-09d32f30c719",
"metadata": {},
"outputs": [],
"source": [
"# Alternative, with apply(). Will be slow for big DataFrames\n",
"def calculate_user_ID(row):\n",
" return f\"{row[\"last_name\"]}{row[\"first_name\"][0]}\"\n",
"\n",
Expand Down Expand Up @@ -511,7 +528,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.0"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 2487b88

Please sign in to comment.