From 9ba45a69849e1cf0e255fc90f7b037816b57df4d Mon Sep 17 00:00:00 2001 From: estcr Date: Tue, 12 Nov 2024 23:29:04 +0100 Subject: [PATCH 1/2] Solucion lab data cleaning and formatting --- lab-dw-data-cleaning-and-formatting.ipynb | 1229 +++++++++++++++------ 1 file changed, 885 insertions(+), 344 deletions(-) diff --git a/lab-dw-data-cleaning-and-formatting.ipynb b/lab-dw-data-cleaning-and-formatting.ipynb index cdfc3c6..65d6634 100644 --- a/lab-dw-data-cleaning-and-formatting.ipynb +++ b/lab-dw-data-cleaning-and-formatting.ipynb @@ -1,371 +1,912 @@ { - "cells": [ - { - "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", - "metadata": { - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e" - }, - "source": [ - "# Lab | Data Cleaning and Formatting" - ] - }, - { - "cell_type": "markdown", - "id": "d1973e9e-8be6-4039-b70e-d73ee0d94c99", - "metadata": { - "id": "d1973e9e-8be6-4039-b70e-d73ee0d94c99" - }, - "source": [ - "In this lab, we will be working with the customer data from an insurance company, which can be found in the CSV file located at the following link: https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file1.csv\n" - ] - }, - { - "cell_type": "markdown", - "id": "31b8a9e7-7db9-4604-991b-ef6771603e57", - "metadata": { - "id": "31b8a9e7-7db9-4604-991b-ef6771603e57" - }, - "source": [ - "# Challenge 1: Data Cleaning and Formatting" - ] - }, - { - "cell_type": "markdown", - "id": "81553f19-9f2c-484b-8940-520aff884022", - "metadata": { - "id": "81553f19-9f2c-484b-8940-520aff884022" - }, - "source": [ - "## Exercise 1: Cleaning Column Names" - ] - }, - { - "cell_type": "markdown", - "id": "34a929f4-1be4-4fa8-adda-42ffd920be90", - "metadata": { - "id": "34a929f4-1be4-4fa8-adda-42ffd920be90" - }, - "source": [ - "To ensure consistency and ease of use, standardize the column names of the dataframe. Start by taking a first look at the dataframe and identifying any column names that need to be modified. Use appropriate naming conventions and make sure that column names are descriptive and informative.\n", - "\n", - "*Hint*:\n", - "- *Column names should be in lower case*\n", - "- *White spaces in column names should be replaced by `_`*\n", - "- *`st` could be replaced for `state`*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5810735c-8056-4442-bbf2-dda38d3e284a", - "metadata": { - "id": "5810735c-8056-4442-bbf2-dda38d3e284a" - }, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "id": "9cb501ec-36ff-4589-b872-6252bb150316", - "metadata": { - "id": "9cb501ec-36ff-4589-b872-6252bb150316" - }, - "source": [ - "## Exercise 2: Cleaning invalid Values" - ] - }, - { - "cell_type": "markdown", - "id": "771fdcf3-8e20-4b06-9c24-3a93ba2b0909", - "metadata": { - "id": "771fdcf3-8e20-4b06-9c24-3a93ba2b0909" - }, - "source": [ - "The dataset contains columns with inconsistent and incorrect values that could affect the accuracy of our analysis. Therefore, we need to clean these columns to ensure that they only contain valid data.\n", - "\n", - "Note that this exercise will focus only on cleaning inconsistent values and will not involve handling null values (NaN or None).\n", - "\n", - "*Hint*:\n", - "- *Gender column contains various inconsistent values such as \"F\", \"M\", \"Femal\", \"Male\", \"female\", which need to be standardized, for example, to \"M\" and \"F\".*\n", - "- *State abbreviations be can replaced with its full name, for example \"AZ\": \"Arizona\", \"Cali\": \"California\", \"WA\": \"Washington\"*\n", - "- *In education, \"Bachelors\" could be replaced by \"Bachelor\"*\n", - "- *In Customer Lifetime Value, delete the `%` character*\n", - "- *In vehicle class, \"Sports Car\", \"Luxury SUV\" and \"Luxury Car\" could be replaced by \"Luxury\"*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c", - "metadata": { - "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c" - }, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "id": "85ff78ce-0174-4890-9db3-8048b7d7d2d0", - "metadata": { - "id": "85ff78ce-0174-4890-9db3-8048b7d7d2d0" - }, - "source": [ - "## Exercise 3: Formatting data types" - ] - }, - { - "cell_type": "markdown", - "id": "b91c2cf8-79a2-4baf-9f65-ff2fb22270bd", - "metadata": { - "id": "b91c2cf8-79a2-4baf-9f65-ff2fb22270bd" - }, - "source": [ - "The data types of many columns in the dataset appear to be incorrect. This could impact the accuracy of our analysis. To ensure accurate analysis, we need to correct the data types of these columns. Please update the data types of the columns as appropriate." - ] - }, - { - "cell_type": "markdown", - "id": "43e5d853-ff9e-43b2-9d92-aef2f78764f3", - "metadata": { - "id": "43e5d853-ff9e-43b2-9d92-aef2f78764f3" - }, - "source": [ - "It is important to note that this exercise does not involve handling null values (NaN or None)." - ] - }, - { - "cell_type": "markdown", - "id": "329ca691-9196-4419-8969-3596746237a1", - "metadata": { - "id": "329ca691-9196-4419-8969-3596746237a1" - }, - "source": [ - "*Hint*:\n", - "- *Customer lifetime value should be numeric*\n", - "- *Number of open complaints has an incorrect format. Look at the different values it takes with `unique()` and take the middle value. As an example, 1/5/00 should be 5. Number of open complaints is a string - remember you can use `split()` to deal with it and take the number you need. Finally, since it should be numeric, cast the column to be in its proper type.*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9", - "metadata": { - "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9" - }, - "outputs": [], - "source": [ - "# Your code here" - ] - }, + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": { + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e" + }, + "source": [ + "# Lab | Data Cleaning and Formatting" + ] + }, + { + "cell_type": "markdown", + "id": "d1973e9e-8be6-4039-b70e-d73ee0d94c99", + "metadata": { + "id": "d1973e9e-8be6-4039-b70e-d73ee0d94c99" + }, + "source": [ + "In this lab, we will be working with the customer data from an insurance company, which can be found in the CSV file located at the following link: https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file1.csv\n" + ] + }, + { + "cell_type": "markdown", + "id": "31b8a9e7-7db9-4604-991b-ef6771603e57", + "metadata": { + "id": "31b8a9e7-7db9-4604-991b-ef6771603e57" + }, + "source": [ + "# Challenge 1: Data Cleaning and Formatting" + ] + }, + { + "cell_type": "markdown", + "id": "81553f19-9f2c-484b-8940-520aff884022", + "metadata": { + "id": "81553f19-9f2c-484b-8940-520aff884022" + }, + "source": [ + "## Exercise 1: Cleaning Column Names" + ] + }, + { + "cell_type": "markdown", + "id": "34a929f4-1be4-4fa8-adda-42ffd920be90", + "metadata": { + "id": "34a929f4-1be4-4fa8-adda-42ffd920be90" + }, + "source": [ + "To ensure consistency and ease of use, standardize the column names of the dataframe. Start by taking a first look at the dataframe and identifying any column names that need to be modified. Use appropriate naming conventions and make sure that column names are descriptive and informative.\n", + "\n", + "*Hint*:\n", + "- *Column names should be in lower case*\n", + "- *White spaces in column names should be replaced by `_`*\n", + "- *`st` could be replaced for `state`*" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5810735c-8056-4442-bbf2-dda38d3e284a", + "metadata": { + "id": "5810735c-8056-4442-bbf2-dda38d3e284a" + }, + "outputs": [ { - "cell_type": "markdown", - "id": "14c52e28-2d0c-4dd2-8bd5-3476e34fadc1", - "metadata": { - "id": "14c52e28-2d0c-4dd2-8bd5-3476e34fadc1" - }, - "source": [ - "## Exercise 4: Dealing with Null values" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 4008 entries, 0 to 4007\n", + "Data columns (total 11 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 Customer 1071 non-null object \n", + " 1 ST 1071 non-null object \n", + " 2 GENDER 954 non-null object \n", + " 3 Education 1071 non-null object \n", + " 4 Customer Lifetime Value 1068 non-null object \n", + " 5 Income 1071 non-null float64\n", + " 6 Monthly Premium Auto 1071 non-null float64\n", + " 7 Number of Open Complaints 1071 non-null object \n", + " 8 Policy Type 1071 non-null object \n", + " 9 Vehicle Class 1071 non-null object \n", + " 10 Total Claim Amount 1071 non-null float64\n", + "dtypes: float64(3), object(8)\n", + "memory usage: 344.6+ KB\n", + "None\n", + " Customer ST GENDER Education Customer Lifetime Value \\\n", + "0 RB50392 Washington NaN Master NaN \n", + "1 QZ44356 Arizona F Bachelor 697953.59% \n", + "2 AI49188 Nevada F Bachelor 1288743.17% \n", + "3 WW63253 California M Bachelor 764586.18% \n", + "4 GA49547 Washington M High School or Below 536307.65% \n", + "\n", + " Income Monthly Premium Auto Number of Open Complaints Policy Type \\\n", + "0 0.0 1000.0 1/0/00 Personal Auto \n", + "1 0.0 94.0 1/0/00 Personal Auto \n", + "2 48767.0 108.0 1/0/00 Personal Auto \n", + "3 0.0 106.0 1/0/00 Corporate Auto \n", + "4 36357.0 68.0 1/0/00 Personal Auto \n", + "\n", + " Vehicle Class Total Claim Amount \n", + "0 Four-Door Car 2.704934 \n", + "1 Four-Door Car 1131.464935 \n", + "2 Two-Door Car 566.472247 \n", + "3 SUV 529.881344 \n", + "4 Four-Door Car 17.269323 \n" + ] + } + ], + "source": [ + "import pandas as pd\n", + "url=\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file1.csv\"\n", + "df=pd.read_csv(url)\n", + "\n", + "print(df.info())\n", + "print(df.head())" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3b6b640a", + "metadata": {}, + "outputs": [], + "source": [ + "df_for_work = df.copy()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f6d6b851", + "metadata": {}, + "outputs": [], + "source": [ + "df_for_work.columns=df_for_work.columns.str.replace(\" \", \"_\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "7aef9dd9", + "metadata": {}, + "outputs": [], + "source": [ + "df_for_work.rename(columns={\"ST\":\"State\"},inplace=True)\n", + "\n", + "df_for_work.rename(columns={\"GENDER\":\"Gender\"},inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "ca31a73d", + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "id": "34b9a20f-7d32-4417-975e-1b4dfb0e16cd", - "metadata": { - "id": "34b9a20f-7d32-4417-975e-1b4dfb0e16cd" - }, - "source": [ - "Identify any columns with null or missing values. Identify how many null values each column has. You can use the `isnull()` function in pandas to find columns with null values.\n", - "\n", - "Decide on a strategy for handling the null values. There are several options, including:\n", - "\n", - "- Drop the rows or columns with null values\n", - "- Fill the null values with a specific value (such as the column mean or median for numerical variables, and mode for categorical variables)\n", - "- Fill the null values with the previous or next value in the column\n", - "- Fill the null values based on a more complex algorithm or model (note: we haven't covered this yet)\n", - "\n", - "Implement your chosen strategy to handle the null values. You can use the `fillna()` function in pandas to fill null values or `dropna()` function to drop null values.\n", - "\n", - "Verify that your strategy has successfully handled the null values. You can use the `isnull()` function again to check if there are still null values in the dataset.\n", - "\n", - "Remember to document your process and explain your reasoning for choosing a particular strategy for handling null values.\n", - "\n", - "After formatting data types, as a last step, convert all the numeric variables to integers." + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
CustomerStateGenderEducationCustomer_Lifetime_ValueIncomeMonthly_Premium_AutoNumber_of_Open_ComplaintsPolicy_TypeVehicle_ClassTotal_Claim_Amount
0RB50392WashingtonNaNMasterNaN0.01000.01/0/00Personal AutoFour-Door Car2.704934
1QZ44356ArizonaFBachelor697953.59%0.094.01/0/00Personal AutoFour-Door Car1131.464935
2AI49188NevadaFBachelor1288743.17%48767.0108.01/0/00Personal AutoTwo-Door Car566.472247
3WW63253CaliforniaMBachelor764586.18%0.0106.01/0/00Corporate AutoSUV529.881344
4GA49547WashingtonMHigh School or Below536307.65%36357.068.01/0/00Personal AutoFour-Door Car17.269323
\n", + "
" + ], + "text/plain": [ + " Customer State Gender Education Customer_Lifetime_Value \\\n", + "0 RB50392 Washington NaN Master NaN \n", + "1 QZ44356 Arizona F Bachelor 697953.59% \n", + "2 AI49188 Nevada F Bachelor 1288743.17% \n", + "3 WW63253 California M Bachelor 764586.18% \n", + "4 GA49547 Washington M High School or Below 536307.65% \n", + "\n", + " Income Monthly_Premium_Auto Number_of_Open_Complaints Policy_Type \\\n", + "0 0.0 1000.0 1/0/00 Personal Auto \n", + "1 0.0 94.0 1/0/00 Personal Auto \n", + "2 48767.0 108.0 1/0/00 Personal Auto \n", + "3 0.0 106.0 1/0/00 Corporate Auto \n", + "4 36357.0 68.0 1/0/00 Personal Auto \n", + "\n", + " Vehicle_Class Total_Claim_Amount \n", + "0 Four-Door Car 2.704934 \n", + "1 Four-Door Car 1131.464935 \n", + "2 Two-Door Car 566.472247 \n", + "3 SUV 529.881344 \n", + "4 Four-Door Car 17.269323 " ] - }, + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_for_work.head()\n" + ] + }, + { + "cell_type": "markdown", + "id": "9cb501ec-36ff-4589-b872-6252bb150316", + "metadata": { + "id": "9cb501ec-36ff-4589-b872-6252bb150316" + }, + "source": [ + "## Exercise 2: Cleaning invalid Values" + ] + }, + { + "cell_type": "markdown", + "id": "771fdcf3-8e20-4b06-9c24-3a93ba2b0909", + "metadata": { + "id": "771fdcf3-8e20-4b06-9c24-3a93ba2b0909" + }, + "source": [ + "The dataset contains columns with inconsistent and incorrect values that could affect the accuracy of our analysis. Therefore, we need to clean these columns to ensure that they only contain valid data.\n", + "\n", + "Note that this exercise will focus only on cleaning inconsistent values and will not involve handling null values (NaN or None).\n", + "\n", + "*Hint*:\n", + "- *Gender column contains various inconsistent values such as \"F\", \"M\", \"Femal\", \"Male\", \"female\", which need to be standardized, for example, to \"M\" and \"F\".*\n", + "- *State abbreviations be can replaced with its full name, for example \"AZ\": \"Arizona\", \"Cali\": \"California\", \"WA\": \"Washington\"*\n", + "- *In education, \"Bachelors\" could be replaced by \"Bachelor\"*\n", + "- *In Customer Lifetime Value, delete the `%` character*\n", + "- *In vehicle class, \"Sports Car\", \"Luxury SUV\" and \"Luxury Car\" could be replaced by \"Luxury\"*" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c", + "metadata": { + "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c" + }, + "outputs": [ { - "cell_type": "code", - "execution_count": null, - "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e", - "metadata": { - "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e" - }, - "outputs": [], - "source": [ - "# Your code here" + "data": { + "text/plain": [ + "array([nan, 'F', 'M'], dtype=object)" ] - }, + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_for_work[\"Gender\"] = df_for_work[\"Gender\"].replace({'Male': 'M', 'Femal': 'F', 'female': 'F'})\n", + "\n", + "df_for_work[\"Gender\"].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "b0d0802b", + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "id": "98416351-e999-4156-9834-9b00a311adfa", - "metadata": { - "id": "98416351-e999-4156-9834-9b00a311adfa" - }, - "source": [ - "## Exercise 5: Dealing with duplicates" + "data": { + "text/plain": [ + "array(['Washington', 'Arizona', 'Nevada', 'California', 'Oregon', nan],\n", + " dtype=object)" ] - }, + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_for_work[\"State\"] = df_for_work[\"State\"].replace({'AZ': 'Arizona', 'WA': 'Washington', 'Cali': 'California'})\n", + "df_for_work[\"State\"].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "7e8c0b8a", + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "id": "ea0816a7-a18e-4d4c-b667-a8452a800bd1", - "metadata": { - "id": "ea0816a7-a18e-4d4c-b667-a8452a800bd1" - }, - "source": [ - "Use the `.duplicated()` method to identify any duplicate rows in the dataframe.\n", - "\n", - "Decide on a strategy for handling the duplicates. Options include:\n", - "- Dropping all duplicate rows\n", - "- Keeping only the first occurrence of each duplicated row\n", - "- Keeping only the last occurrence of each duplicated row\n", - "- Dropping duplicates based on a subset of columns\n", - "- Dropping duplicates based on a specific column\n", - "\n", - "Implement your chosen strategy using the `drop_duplicates()` function.\n", - "\n", - "Verify that your strategy has successfully handled the duplicates by checking for duplicates again using `.duplicated()`.\n", - "\n", - "Remember to document your process and explain your reasoning for choosing a particular strategy for handling duplicates.\n", - "\n", - "Save the cleaned dataset to a new CSV file.\n", - "\n", - "*Hint*: *after dropping duplicates, reset the index to ensure consistency*." + "data": { + "text/plain": [ + "array(['Master', 'Bachelor', 'High School or Below', 'College', 'Doctor',\n", + " nan], dtype=object)" ] - }, + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_for_work[\"Education\"] = df_for_work[\"Education\"].replace({'Bachelors': 'Bachelor'})\n", + "df_for_work[\"Education\"].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "1a4f8b29", + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": null, - "id": "1929362c-47ed-47cb-baca-358b78d401a0", - "metadata": { - "id": "1929362c-47ed-47cb-baca-358b78d401a0" - }, - "outputs": [], - "source": [ - "# Your code here" + "data": { + "text/plain": [ + "array([nan, '697953.59', '1288743.17', ..., '2031499.76', '323912.47',\n", + " '899704.02'], dtype=object)" ] - }, + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "df_for_work[\"Customer_Lifetime_Value\"] = df_for_work[\"Customer_Lifetime_Value\"].str.replace('%', '', regex=False)\n", + "df_for_work[\"Customer_Lifetime_Value\"].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "2011b051", + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "id": "60840701-4783-40e2-b4d8-55303f9100c9", - "metadata": { - "id": "60840701-4783-40e2-b4d8-55303f9100c9" - }, - "source": [ - "# Bonus: Challenge 2: creating functions on a separate `py` file" + "data": { + "text/plain": [ + "array(['Four-Door Car', 'Two-Door Car', 'SUV', 'Luxury SUV', 'Luxury',\n", + " nan], dtype=object)" ] - }, + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_for_work[\"Vehicle_Class\"] = df_for_work[\"Vehicle_Class\"].replace({'Sports Car': 'Luxury','Luxury Suv':'Luxury','Luxury Car' : 'Luxury'})\n", + "df_for_work[\"Vehicle_Class\"].unique()" + ] + }, + { + "cell_type": "markdown", + "id": "85ff78ce-0174-4890-9db3-8048b7d7d2d0", + "metadata": { + "id": "85ff78ce-0174-4890-9db3-8048b7d7d2d0" + }, + "source": [ + "## Exercise 3: Formatting data types" + ] + }, + { + "cell_type": "markdown", + "id": "b91c2cf8-79a2-4baf-9f65-ff2fb22270bd", + "metadata": { + "id": "b91c2cf8-79a2-4baf-9f65-ff2fb22270bd" + }, + "source": [ + "The data types of many columns in the dataset appear to be incorrect. This could impact the accuracy of our analysis. To ensure accurate analysis, we need to correct the data types of these columns. Please update the data types of the columns as appropriate." + ] + }, + { + "cell_type": "markdown", + "id": "43e5d853-ff9e-43b2-9d92-aef2f78764f3", + "metadata": { + "id": "43e5d853-ff9e-43b2-9d92-aef2f78764f3" + }, + "source": [ + "It is important to note that this exercise does not involve handling null values (NaN or None)." + ] + }, + { + "cell_type": "markdown", + "id": "329ca691-9196-4419-8969-3596746237a1", + "metadata": { + "id": "329ca691-9196-4419-8969-3596746237a1" + }, + "source": [ + "*Hint*:\n", + "- *Customer lifetime value should be numeric*\n", + "- *Number of open complaints has an incorrect format. Look at the different values it takes with `unique()` and take the middle value. As an example, 1/5/00 should be 5. Number of open complaints is a string - remember you can use `split()` to deal with it and take the number you need. Finally, since it should be numeric, cast the column to be in its proper type.*" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9", + "metadata": { + "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9" + }, + "outputs": [ { - "cell_type": "markdown", - "id": "9d1adb3a-17cf-4899-8041-da21a4337fb4", - "metadata": { - "id": "9d1adb3a-17cf-4899-8041-da21a4337fb4" - }, - "source": [ - "Put all the data cleaning and formatting steps into functions, and create a main function that performs all the cleaning and formatting.\n", - "\n", - "Write these functions in separate .py file(s). By putting these steps into functions, we can make the code more modular and easier to maintain." - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 4008 entries, 0 to 4007\n", + "Data columns (total 11 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 Customer 1071 non-null object \n", + " 1 State 1071 non-null object \n", + " 2 Gender 954 non-null object \n", + " 3 Education 1071 non-null object \n", + " 4 Customer_Lifetime_Value 1068 non-null float64\n", + " 5 Income 1071 non-null float64\n", + " 6 Monthly_Premium_Auto 1071 non-null float64\n", + " 7 Number_of_Open_Complaints 1071 non-null object \n", + " 8 Policy_Type 1071 non-null object \n", + " 9 Vehicle_Class 1071 non-null object \n", + " 10 Total_Claim_Amount 1071 non-null float64\n", + "dtypes: float64(4), object(7)\n", + "memory usage: 344.6+ KB\n" + ] + } + ], + "source": [ + "df_for_work[\"Customer_Lifetime_Value\"] = df_for_work [\"Customer_Lifetime_Value\"].astype(float)\n", + "df_for_work.info()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "67bdc61f", + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "id": "0e170dc2-b62c-417a-8248-e63ed18a70c4", - "metadata": { - "id": "0e170dc2-b62c-417a-8248-e63ed18a70c4" - }, - "source": [ - "*Hint: autoreload module is a utility module in Python that allows you to automatically reload modules in the current session when changes are made to the source code. This can be useful in situations where you are actively developing code and want to see the effects of changes you make without having to constantly restart the Python interpreter or Jupyter Notebook kernel.*" + "data": { + "text/plain": [ + "array(['1/0/00', '1/2/00', '1/1/00', '1/3/00', '1/5/00', '1/4/00', nan],\n", + " dtype=object)" ] - }, + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_for_work[\"Number_of_Open_Complaints\"].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "84e0557e", + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": null, - "id": "a52c6dfc-cd11-4d01-bda4-f719fa33e9a4", - "metadata": { - "id": "a52c6dfc-cd11-4d01-bda4-f719fa33e9a4" - }, - "outputs": [], - "source": [ - "# Your code here" + "data": { + "text/plain": [ + "array([ 0., 2., 1., 3., 5., 4., nan])" ] - }, + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Lo Tuve que hacer con str pq me generaba el error ya que eran todos nro.\n", + "df_for_work[\"Number_of_Open_Complaints\"] = df_for_work[\"Number_of_Open_Complaints\"].str.split('/').str[1]\n", + "\n", + "df_for_work[\"Number_of_Open_Complaints\"] = pd.to_numeric(df_for_work[\"Number_of_Open_Complaints\"], errors='coerce')\n", + "\n", + "\n", + "df_for_work[\"Number_of_Open_Complaints\"].unique()" + ] + }, + { + "cell_type": "markdown", + "id": "14c52e28-2d0c-4dd2-8bd5-3476e34fadc1", + "metadata": { + "id": "14c52e28-2d0c-4dd2-8bd5-3476e34fadc1" + }, + "source": [ + "## Exercise 4: Dealing with Null values" + ] + }, + { + "cell_type": "markdown", + "id": "34b9a20f-7d32-4417-975e-1b4dfb0e16cd", + "metadata": { + "id": "34b9a20f-7d32-4417-975e-1b4dfb0e16cd" + }, + "source": [ + "Identify any columns with null or missing values. Identify how many null values each column has. You can use the `isnull()` function in pandas to find columns with null values.\n", + "\n", + "Decide on a strategy for handling the null values. There are several options, including:\n", + "\n", + "- Drop the rows or columns with null values\n", + "- Fill the null values with a specific value (such as the column mean or median for numerical variables, and mode for categorical variables)\n", + "- Fill the null values with the previous or next value in the column\n", + "- Fill the null values based on a more complex algorithm or model (note: we haven't covered this yet)\n", + "\n", + "Implement your chosen strategy to handle the null values. You can use the `fillna()` function in pandas to fill null values or `dropna()` function to drop null values.\n", + "\n", + "Verify that your strategy has successfully handled the null values. You can use the `isnull()` function again to check if there are still null values in the dataset.\n", + "\n", + "Remember to document your process and explain your reasoning for choosing a particular strategy for handling null values.\n", + "\n", + "After formatting data types, as a last step, convert all the numeric variables to integers." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e", + "metadata": { + "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e" + }, + "outputs": [ { - "cell_type": "markdown", - "id": "80f846bb-3f5e-4ca2-96c0-900728daca5a", - "metadata": { - "tags": [], - "id": "80f846bb-3f5e-4ca2-96c0-900728daca5a" - }, - "source": [ - "# Bonus: Challenge 3: Analyzing Clean and Formated Data" + "data": { + "text/plain": [ + "State\n", + "True 2937\n", + "False 1071\n", + "Name: count, dtype: int64" ] - }, + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_for_work[\"State\"].isnull().value_counts()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "d615f782", + "metadata": {}, + "outputs": [], + "source": [ + "df_for_work.dropna(how=\"all\", inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "30267664", + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "id": "9021630e-cc90-446c-b5bd-264d6c864207", - "metadata": { - "id": "9021630e-cc90-446c-b5bd-264d6c864207" - }, - "source": [ - "You have been tasked with analyzing the data to identify potential areas for improving customer retention and profitability. Your goal is to identify customers with a high policy claim amount and a low customer lifetime value.\n", - "\n", - "In the Pandas Lab, we only looked at high policy claim amounts because we couldn't look into low customer lifetime values. If we had tried to work with that column, we wouldn't have been able to because customer lifetime value wasn't clean and in its proper format. So after cleaning and formatting the data, let's get some more interesting insights!\n", - "\n", - "Instructions:\n", - "\n", - "- Review the statistics again for total claim amount and customer lifetime value to gain an understanding of the data.\n", - "- To identify potential areas for improving customer retention and profitability, we want to focus on customers with a high policy claim amount and a low customer lifetime value. Consider customers with a high policy claim amount to be those in the top 25% of the total claim amount, and clients with a low customer lifetime value to be those in the bottom 25% of the customer lifetime value. Create a pandas DataFrame object that contains information about customers with a policy claim amount greater than the 75th percentile and a customer lifetime value in the bottom 25th percentile.\n", - "- Use DataFrame methods to calculate summary statistics about the high policy claim amount and low customer lifetime value data. To do so, select both columns of the dataframe simultaneously and pass it to the `.describe()` method. This will give you descriptive statistics, such as mean, median, standard deviation, minimum and maximum values for both columns at the same time, allowing you to compare and analyze their characteristics." - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Index: 1071 entries, 0 to 1070\n", + "Data columns (total 11 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 Customer 1071 non-null object \n", + " 1 State 1071 non-null object \n", + " 2 Gender 954 non-null object \n", + " 3 Education 1071 non-null object \n", + " 4 Customer_Lifetime_Value 1068 non-null float64\n", + " 5 Income 1071 non-null float64\n", + " 6 Monthly_Premium_Auto 1071 non-null float64\n", + " 7 Number_of_Open_Complaints 1071 non-null float64\n", + " 8 Policy_Type 1071 non-null object \n", + " 9 Vehicle_Class 1071 non-null object \n", + " 10 Total_Claim_Amount 1071 non-null float64\n", + "dtypes: float64(5), object(6)\n", + "memory usage: 100.4+ KB\n" + ] + } + ], + "source": [ + "df_for_work.info()" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "09df880f", + "metadata": {}, + "outputs": [], + "source": [ + "(df_for_work[\"Customer_Lifetime_Value\"].isnull().value_counts()) #Son solo 3 no afectaria en el total se pueden borrar\n", + "\n", + "df_for_work = df_for_work.dropna(subset=[\"Customer_Lifetime_Value\"])" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "39650f16", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "df_for_work[\"Gender\"] = df_for_work[\"Gender\"].fillna(\"Unknown\")" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "d20ef4b3", + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": null, - "id": "211e82b5-461a-4d6f-8a23-4deccb84173c", - "metadata": { - "id": "211e82b5-461a-4d6f-8a23-4deccb84173c" - }, - "outputs": [], - "source": [ - "# Your code here" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Index: 1068 entries, 1 to 1070\n", + "Data columns (total 11 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 Customer 1068 non-null object \n", + " 1 State 1068 non-null object \n", + " 2 Gender 1068 non-null object \n", + " 3 Education 1068 non-null object \n", + " 4 Customer_Lifetime_Value 1068 non-null float64\n", + " 5 Income 1068 non-null float64\n", + " 6 Monthly_Premium_Auto 1068 non-null float64\n", + " 7 Number_of_Open_Complaints 1068 non-null float64\n", + " 8 Policy_Type 1068 non-null object \n", + " 9 Vehicle_Class 1068 non-null object \n", + " 10 Total_Claim_Amount 1068 non-null float64\n", + "dtypes: float64(5), object(6)\n", + "memory usage: 100.1+ KB\n" + ] } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.13" - }, - "colab": { - "provenance": [] + ], + "source": [ + "df_for_work.info()" + ] + }, + { + "cell_type": "markdown", + "id": "98416351-e999-4156-9834-9b00a311adfa", + "metadata": { + "id": "98416351-e999-4156-9834-9b00a311adfa" + }, + "source": [ + "## Exercise 5: Dealing with duplicates" + ] + }, + { + "cell_type": "markdown", + "id": "ea0816a7-a18e-4d4c-b667-a8452a800bd1", + "metadata": { + "id": "ea0816a7-a18e-4d4c-b667-a8452a800bd1" + }, + "source": [ + "Use the `.duplicated()` method to identify any duplicate rows in the dataframe.\n", + "\n", + "Decide on a strategy for handling the duplicates. Options include:\n", + "- Dropping all duplicate rows\n", + "- Keeping only the first occurrence of each duplicated row\n", + "- Keeping only the last occurrence of each duplicated row\n", + "- Dropping duplicates based on a subset of columns\n", + "- Dropping duplicates based on a specific column\n", + "\n", + "Implement your chosen strategy using the `drop_duplicates()` function.\n", + "\n", + "Verify that your strategy has successfully handled the duplicates by checking for duplicates again using `.duplicated()`.\n", + "\n", + "Remember to document your process and explain your reasoning for choosing a particular strategy for handling duplicates.\n", + "\n", + "Save the cleaned dataset to a new CSV file.\n", + "\n", + "*Hint*: *after dropping duplicates, reset the index to ensure consistency*." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "1929362c-47ed-47cb-baca-358b78d401a0", + "metadata": { + "id": "1929362c-47ed-47cb-baca-358b78d401a0" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Número de filas duplicadas: 0\n", + "Empty DataFrame\n", + "Columns: [Customer, State, Gender, Education, Customer_Lifetime_Value, Income, Monthly_Premium_Auto, Number_of_Open_Complaints, Policy_Type, Vehicle_Class, Total_Claim_Amount]\n", + "Index: []\n" + ] } + ], + "source": [ + "duplicados = df_for_work[df_for_work.duplicated()]\n", + "print(\"Número de filas duplicadas:\", len(duplicados))\n", + "print(duplicados)" + ] + }, + { + "cell_type": "markdown", + "id": "60840701-4783-40e2-b4d8-55303f9100c9", + "metadata": { + "id": "60840701-4783-40e2-b4d8-55303f9100c9" + }, + "source": [ + "# Bonus: Challenge 2: creating functions on a separate `py` file" + ] + }, + { + "cell_type": "markdown", + "id": "9d1adb3a-17cf-4899-8041-da21a4337fb4", + "metadata": { + "id": "9d1adb3a-17cf-4899-8041-da21a4337fb4" + }, + "source": [ + "Put all the data cleaning and formatting steps into functions, and create a main function that performs all the cleaning and formatting.\n", + "\n", + "Write these functions in separate .py file(s). By putting these steps into functions, we can make the code more modular and easier to maintain." + ] + }, + { + "cell_type": "markdown", + "id": "0e170dc2-b62c-417a-8248-e63ed18a70c4", + "metadata": { + "id": "0e170dc2-b62c-417a-8248-e63ed18a70c4" + }, + "source": [ + "*Hint: autoreload module is a utility module in Python that allows you to automatically reload modules in the current session when changes are made to the source code. This can be useful in situations where you are actively developing code and want to see the effects of changes you make without having to constantly restart the Python interpreter or Jupyter Notebook kernel.*" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "a52c6dfc-cd11-4d01-bda4-f719fa33e9a4", + "metadata": { + "id": "a52c6dfc-cd11-4d01-bda4-f719fa33e9a4" + }, + "outputs": [], + "source": [ + "# Your code here" + ] + }, + { + "cell_type": "markdown", + "id": "80f846bb-3f5e-4ca2-96c0-900728daca5a", + "metadata": { + "id": "80f846bb-3f5e-4ca2-96c0-900728daca5a", + "tags": [] + }, + "source": [ + "# Bonus: Challenge 3: Analyzing Clean and Formated Data" + ] + }, + { + "cell_type": "markdown", + "id": "9021630e-cc90-446c-b5bd-264d6c864207", + "metadata": { + "id": "9021630e-cc90-446c-b5bd-264d6c864207" + }, + "source": [ + "You have been tasked with analyzing the data to identify potential areas for improving customer retention and profitability. Your goal is to identify customers with a high policy claim amount and a low customer lifetime value.\n", + "\n", + "In the Pandas Lab, we only looked at high policy claim amounts because we couldn't look into low customer lifetime values. If we had tried to work with that column, we wouldn't have been able to because customer lifetime value wasn't clean and in its proper format. So after cleaning and formatting the data, let's get some more interesting insights!\n", + "\n", + "Instructions:\n", + "\n", + "- Review the statistics again for total claim amount and customer lifetime value to gain an understanding of the data.\n", + "- To identify potential areas for improving customer retention and profitability, we want to focus on customers with a high policy claim amount and a low customer lifetime value. Consider customers with a high policy claim amount to be those in the top 25% of the total claim amount, and clients with a low customer lifetime value to be those in the bottom 25% of the customer lifetime value. Create a pandas DataFrame object that contains information about customers with a policy claim amount greater than the 75th percentile and a customer lifetime value in the bottom 25th percentile.\n", + "- Use DataFrame methods to calculate summary statistics about the high policy claim amount and low customer lifetime value data. To do so, select both columns of the dataframe simultaneously and pass it to the `.describe()` method. This will give you descriptive statistics, such as mean, median, standard deviation, minimum and maximum values for both columns at the same time, allowing you to compare and analyze their characteristics." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "211e82b5-461a-4d6f-8a23-4deccb84173c", + "metadata": { + "id": "211e82b5-461a-4d6f-8a23-4deccb84173c" + }, + "outputs": [], + "source": [ + "# Your code here" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" }, - "nbformat": 4, - "nbformat_minor": 5 + "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.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 } From c39fde340e921ef0fe1235c552ef606138a0aba6 Mon Sep 17 00:00:00 2001 From: estcr Date: Tue, 12 Nov 2024 23:35:35 +0100 Subject: [PATCH 2/2] Solucion lab cleaning and formatting Se agregaron descripciones --- lab-dw-data-cleaning-and-formatting.ipynb | 46 ++++++++++++++++------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/lab-dw-data-cleaning-and-formatting.ipynb b/lab-dw-data-cleaning-and-formatting.ipynb index 65d6634..be0551f 100644 --- a/lab-dw-data-cleaning-and-formatting.ipynb +++ b/lab-dw-data-cleaning-and-formatting.ipynb @@ -130,21 +130,23 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "f6d6b851", "metadata": {}, "outputs": [], "source": [ + "#Se sacan los espacios por _ en los nombres de columnas\n", "df_for_work.columns=df_for_work.columns.str.replace(\" \", \"_\")" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "7aef9dd9", "metadata": {}, "outputs": [], "source": [ + "#Cambio del nombre de las columnas\n", "df_for_work.rename(columns={\"ST\":\"State\"},inplace=True)\n", "\n", "df_for_work.rename(columns={\"GENDER\":\"Gender\"},inplace=True)" @@ -328,7 +330,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c", "metadata": { "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c" @@ -346,6 +348,7 @@ } ], "source": [ + "#Correción de los datos\n", "df_for_work[\"Gender\"] = df_for_work[\"Gender\"].replace({'Male': 'M', 'Femal': 'F', 'female': 'F'})\n", "\n", "df_for_work[\"Gender\"].unique()" @@ -353,7 +356,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "b0d0802b", "metadata": {}, "outputs": [ @@ -370,13 +373,14 @@ } ], "source": [ + "#Correción de los datos\n", "df_for_work[\"State\"] = df_for_work[\"State\"].replace({'AZ': 'Arizona', 'WA': 'Washington', 'Cali': 'California'})\n", "df_for_work[\"State\"].unique()" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "7e8c0b8a", "metadata": {}, "outputs": [ @@ -393,13 +397,14 @@ } ], "source": [ + "#Correción de los datos\n", "df_for_work[\"Education\"] = df_for_work[\"Education\"].replace({'Bachelors': 'Bachelor'})\n", "df_for_work[\"Education\"].unique()" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "1a4f8b29", "metadata": {}, "outputs": [ @@ -416,14 +421,14 @@ } ], "source": [ - "\n", + "#Correción de los datos\n", "df_for_work[\"Customer_Lifetime_Value\"] = df_for_work[\"Customer_Lifetime_Value\"].str.replace('%', '', regex=False)\n", "df_for_work[\"Customer_Lifetime_Value\"].unique()" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "2011b051", "metadata": {}, "outputs": [ @@ -440,6 +445,7 @@ } ], "source": [ + "#Correción de los datos\n", "df_for_work[\"Vehicle_Class\"] = df_for_work[\"Vehicle_Class\"].replace({'Sports Car': 'Luxury','Luxury Suv':'Luxury','Luxury Car' : 'Luxury'})\n", "df_for_work[\"Vehicle_Class\"].unique()" ] @@ -488,7 +494,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9", "metadata": { "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9" @@ -520,6 +526,7 @@ } ], "source": [ + "#Se cambia el tipo de dato de la columnna\n", "df_for_work[\"Customer_Lifetime_Value\"] = df_for_work [\"Customer_Lifetime_Value\"].astype(float)\n", "df_for_work.info()" ] @@ -636,11 +643,12 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "d615f782", "metadata": {}, "outputs": [], "source": [ + "#Borrar todas las filas que son completamente null\n", "df_for_work.dropna(how=\"all\", inplace=True)" ] }, @@ -681,11 +689,12 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "09df880f", "metadata": {}, "outputs": [], "source": [ + "#Son solo 3 los null por lo que no afectaria en el total, los mismos fueron borrados\n", "(df_for_work[\"Customer_Lifetime_Value\"].isnull().value_counts()) #Son solo 3 no afectaria en el total se pueden borrar\n", "\n", "df_for_work = df_for_work.dropna(subset=[\"Customer_Lifetime_Value\"])" @@ -693,12 +702,12 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "39650f16", "metadata": {}, "outputs": [], "source": [ - "\n", + "# Llenar los valores nulos en la columna Gender\n", "df_for_work[\"Gender\"] = df_for_work[\"Gender\"].fillna(\"Unknown\")" ] }, @@ -799,6 +808,17 @@ "print(duplicados)" ] }, + { + "cell_type": "code", + "execution_count": 26, + "id": "71d3415c", + "metadata": {}, + "outputs": [], + "source": [ + "# Eliminar duplicados y restablecer el índice\n", + "df = df.drop_duplicates().reset_index(drop=True)" + ] + }, { "cell_type": "markdown", "id": "60840701-4783-40e2-b4d8-55303f9100c9",