diff --git a/pandas-reset-index/Main_Code.ipynb b/pandas-reset-index/Main_Code.ipynb index 955a2a6c5d..0fc2592f49 100644 --- a/pandas-reset-index/Main_Code.ipynb +++ b/pandas-reset-index/Main_Code.ipynb @@ -15,7 +15,7 @@ "metadata": {}, "outputs": [], "source": [ - "!Python -m pip install pandas pyarrow" + "!python -m pip install pandas pyarrow" ] }, { @@ -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", + "```" ] }, { @@ -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", + "```" ] }, { @@ -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" ] }, @@ -456,7 +460,7 @@ "metadata": {}, "outputs": [], "source": [ - "beach_boys.loc[[\"Employee_3\"]]" + "beach_boys.loc[[\"Employee_4\"]]" ] }, { @@ -631,7 +635,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.0" + "version": "3.12.7" } }, "nbformat": 4, diff --git a/pandas-reset-index/README.md b/pandas-reset-index/README.md index e568286d6e..5e55c857a1 100644 --- a/pandas-reset-index/README.md +++ b/pandas-reset-index/README.md @@ -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. diff --git a/pandas-reset-index/Solutions.ipynb b/pandas-reset-index/Solutions.ipynb index f0663606ab..4e6475ce60 100644 --- a/pandas-reset-index/Solutions.ipynb +++ b/pandas-reset-index/Solutions.ipynb @@ -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" ] @@ -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" ] }, @@ -274,6 +276,20 @@ ").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, @@ -281,6 +297,7 @@ "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", @@ -511,7 +528,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.0" + "version": "3.12.7" } }, "nbformat": 4,