From 26653c320523103cf5ff7a7d1f9c5955364e54ec Mon Sep 17 00:00:00 2001 From: JuanLillo Date: Wed, 4 Dec 2024 23:04:49 +0100 Subject: [PATCH] W2L1 done --- lab-dw-data-cleaning-and-formatting.ipynb | 2327 ++++++++++++++++++--- 1 file changed, 1989 insertions(+), 338 deletions(-) diff --git a/lab-dw-data-cleaning-and-formatting.ipynb b/lab-dw-data-cleaning-and-formatting.ipynb index cdfc3c6..13d5228 100644 --- a/lab-dw-data-cleaning-and-formatting.ipynb +++ b/lab-dw-data-cleaning-and-formatting.ipynb @@ -1,371 +1,2022 @@ { - "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\"*" - ] - }, + "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": 45, + "id": "5810735c-8056-4442-bbf2-dda38d3e284a", + "metadata": { + "id": "5810735c-8056-4442-bbf2-dda38d3e284a" + }, + "outputs": [ { - "cell_type": "code", - "execution_count": null, - "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c", - "metadata": { - "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c" - }, - "outputs": [], - "source": [ - "# Your code here" + "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", + " \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", + "
CustomerSTGENDEREducationCustomer 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
....................................
4003NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4004NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4005NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4006NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4007NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
\n", + "

4008 rows × 11 columns

\n", + "
" + ], + "text/plain": [ + " Customer ST GENDER Education \\\n", + "0 RB50392 Washington NaN Master \n", + "1 QZ44356 Arizona F Bachelor \n", + "2 AI49188 Nevada F Bachelor \n", + "3 WW63253 California M Bachelor \n", + "4 GA49547 Washington M High School or Below \n", + "... ... ... ... ... \n", + "4003 NaN NaN NaN NaN \n", + "4004 NaN NaN NaN NaN \n", + "4005 NaN NaN NaN NaN \n", + "4006 NaN NaN NaN NaN \n", + "4007 NaN NaN NaN NaN \n", + "\n", + " Customer Lifetime Value Income Monthly Premium Auto \\\n", + "0 NaN 0.0 1000.0 \n", + "1 697953.59% 0.0 94.0 \n", + "2 1288743.17% 48767.0 108.0 \n", + "3 764586.18% 0.0 106.0 \n", + "4 536307.65% 36357.0 68.0 \n", + "... ... ... ... \n", + "4003 NaN NaN NaN \n", + "4004 NaN NaN NaN \n", + "4005 NaN NaN NaN \n", + "4006 NaN NaN NaN \n", + "4007 NaN NaN NaN \n", + "\n", + " Number of Open Complaints Policy Type Vehicle Class \\\n", + "0 1/0/00 Personal Auto Four-Door Car \n", + "1 1/0/00 Personal Auto Four-Door Car \n", + "2 1/0/00 Personal Auto Two-Door Car \n", + "3 1/0/00 Corporate Auto SUV \n", + "4 1/0/00 Personal Auto Four-Door Car \n", + "... ... ... ... \n", + "4003 NaN NaN NaN \n", + "4004 NaN NaN NaN \n", + "4005 NaN NaN NaN \n", + "4006 NaN NaN NaN \n", + "4007 NaN NaN NaN \n", + "\n", + " Total Claim Amount \n", + "0 2.704934 \n", + "1 1131.464935 \n", + "2 566.472247 \n", + "3 529.881344 \n", + "4 17.269323 \n", + "... ... \n", + "4003 NaN \n", + "4004 NaN \n", + "4005 NaN \n", + "4006 NaN \n", + "4007 NaN \n", + "\n", + "[4008 rows x 11 columns]" ] - }, + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "70c4b25c-26ea-41c4-b3a9-1cc328b9c72d", + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "id": "85ff78ce-0174-4890-9db3-8048b7d7d2d0", - "metadata": { - "id": "85ff78ce-0174-4890-9db3-8048b7d7d2d0" - }, - "source": [ - "## Exercise 3: Formatting data types" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['customer', 'ST', 'GENDER', 'Education', 'Customer Lifetime Value',\n", + " 'Income', 'Monthly Premium Auto', 'Number of Open Complaints',\n", + " 'Policy Type', 'Vehicle Class', 'Total Claim Amount'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "df.rename(columns= {\"Customer\": \"customer\",}, inplace = True)\n", + "print(df.columns)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "0dde54ab-d220-48ba-981a-16216248141a", + "metadata": {}, + "outputs": [ { - "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." - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['customer', 'ST', 'GENDER', 'Education', 'Customer Lifetime Value',\n", + " 'Income', 'Monthly Premium Auto', 'Number of Open Complaints',\n", + " 'Policy Type', 'Vehicle Class', 'Total Claim Amount'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "df.rename(columns= {\"Customer\": \"customer\",}, inplace = True)\n", + "print(df.columns)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "34d2ec5a-34c3-4cfb-a981-585c8c51751b", + "metadata": {}, + "outputs": [ { - "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)." - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['customer', 'state', 'GENDER', 'Education', 'Customer Lifetime Value',\n", + " 'Income', 'Monthly Premium Auto', 'Number of Open Complaints',\n", + " 'Policy Type', 'Vehicle Class', 'Total Claim Amount'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "df.rename(columns= {\"ST\" : \"state\", }, inplace=True)\n", + "print(df.columns)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "8eb82715-3846-4886-b57e-a45b30c2ad26", + "metadata": {}, + "outputs": [ { - "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.*" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['customer', 'state', 'gender', 'Education', 'Customer Lifetime Value',\n", + " 'Income', 'Monthly Premium Auto', 'Number of Open Complaints',\n", + " 'Policy Type', 'Vehicle Class', 'Total Claim Amount'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "df.rename(columns= {\"GENDER\" : \"gender\", }, inplace=True)\n", + "print(df.columns)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "1b370555-3180-4235-ae0f-85c57c9dda02", + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": null, - "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9", - "metadata": { - "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9" - }, - "outputs": [], - "source": [ - "# Your code here" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['customer', 'state', 'gender', 'education', 'Customer Lifetime Value',\n", + " 'Income', 'Monthly Premium Auto', 'Number of Open Complaints',\n", + " 'Policy Type', 'Vehicle Class', 'Total Claim Amount'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "df.rename(columns= {\"Education\" : \"education\", }, inplace=True)\n", + "print(df.columns)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "708150fe-ec85-4b37-8d07-4cf06345ca6c", + "metadata": {}, + "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": [ + "Index(['customer', 'state', 'gender', 'education', 'customer_lifetime_value',\n", + " 'Income', 'Monthly Premium Auto', 'Number of Open Complaints',\n", + " 'Policy Type', 'Vehicle Class', 'Total Claim Amount'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "df.rename(columns= {\"Customer Lifetime Value\" : \"customer_lifetime_value\", }, inplace=True)\n", + "print(df.columns)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "65d669aa-cb6e-47e2-9a84-4c145c77eb49", + "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." - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['customer', 'state', 'gender', 'education', 'customer_lifetime_value',\n", + " 'income', 'Monthly Premium Auto', 'Number of Open Complaints',\n", + " 'Policy Type', 'Vehicle Class', 'Total Claim Amount'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "df.rename(columns= {\"Income\" : \"income\", }, inplace=True)\n", + "print(df.columns)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "b3daf501-6aca-4276-8466-def23c4d4ae0", + "metadata": {}, + "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" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['customer', 'state', 'gender', 'education', 'customer_lifetime_value',\n", + " 'income', 'monthly_premium_auto', 'Number of Open Complaints',\n", + " 'Policy Type', 'Vehicle Class', 'Total Claim Amount'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "df.rename(columns= {\"Monthly Premium Auto\" : \"monthly_premium_auto\", }, inplace=True)\n", + "print(df.columns)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "f311bf53-70fb-4227-910c-e63ea6a71e99", + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "id": "98416351-e999-4156-9834-9b00a311adfa", - "metadata": { - "id": "98416351-e999-4156-9834-9b00a311adfa" - }, - "source": [ - "## Exercise 5: Dealing with duplicates" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['customer', 'state', 'gender', 'education', 'customer_lifetime_value',\n", + " 'income', 'monthly_premium_auto', 'number_of_open_complaints',\n", + " 'Policy Type', 'vehicle_class', 'total_claim_amount'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "df.rename(columns= {\"Number of Open Complaints\" : \"number_of_open_complaints\", \"Policy_Type\" : \"policy_type\", \"Vehicle Class\" : \"vehicle_class\", \"Total Claim Amount\" : \"total_claim_amount\", }, inplace=True)\n", + "print(df.columns)" + ] + }, + { + "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": 79, + "id": "c245b658-ae83-4521-86e0-98a2b2a274f8", + "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/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", + " \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
....................................
4003NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4004NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4005NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4006NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4007NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
\n", + "

4008 rows × 11 columns

\n", + "
" + ], + "text/plain": [ + " customer state gender education \\\n", + "0 RB50392 Washington NaN Master \n", + "1 QZ44356 Arizona F Bachelor \n", + "2 AI49188 Nevada F Bachelor \n", + "3 WW63253 California M Bachelor \n", + "4 GA49547 Washington M High School or Below \n", + "... ... ... ... ... \n", + "4003 NaN NaN NaN NaN \n", + "4004 NaN NaN NaN NaN \n", + "4005 NaN NaN NaN NaN \n", + "4006 NaN NaN NaN NaN \n", + "4007 NaN NaN NaN NaN \n", + "\n", + " customer_lifetime_value income monthly_premium_auto \\\n", + "0 NaN 0.0 1000.0 \n", + "1 697953.59% 0.0 94.0 \n", + "2 1288743.17% 48767.0 108.0 \n", + "3 764586.18% 0.0 106.0 \n", + "4 536307.65% 36357.0 68.0 \n", + "... ... ... ... \n", + "4003 NaN NaN NaN \n", + "4004 NaN NaN NaN \n", + "4005 NaN NaN NaN \n", + "4006 NaN NaN NaN \n", + "4007 NaN NaN NaN \n", + "\n", + " number_of_open_complaints Policy Type vehicle_class \\\n", + "0 1/0/00 Personal Auto Four-Door Car \n", + "1 1/0/00 Personal Auto Four-Door Car \n", + "2 1/0/00 Personal Auto Two-Door Car \n", + "3 1/0/00 Corporate Auto SUV \n", + "4 1/0/00 Personal Auto Four-Door Car \n", + "... ... ... ... \n", + "4003 NaN NaN NaN \n", + "4004 NaN NaN NaN \n", + "4005 NaN NaN NaN \n", + "4006 NaN NaN NaN \n", + "4007 NaN NaN NaN \n", + "\n", + " total_claim_amount \n", + "0 2.704934 \n", + "1 1131.464935 \n", + "2 566.472247 \n", + "3 529.881344 \n", + "4 17.269323 \n", + "... ... \n", + "4003 NaN \n", + "4004 NaN \n", + "4005 NaN \n", + "4006 NaN \n", + "4007 NaN \n", + "\n", + "[4008 rows x 11 columns]" ] - }, + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.replace({\"Female\": \"F\", \"Male\": \"M\", \"AZ\" : \"Arizona\", \"Cali\" : \"California\", \"Wa\": \"Wasshington\", \"Bachelors\" : \"Bachelor\", \"Sports Car\": \"Luxury\", \"Luxury SUV\": \"Luxury\", \"Luxury Car\": \"Luxury\",})\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "f7ef2596-fe65-473c-ba77-df66b88e90fc", + "metadata": {}, + "outputs": [], + "source": [ + "df['customer_lifetime_value'] = df['customer_lifetime_value'].str.replace('%', '')" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "84e15842-604a-44c8-a8b7-1d1b56a56c64", + "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/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", + " \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.590.094.01/0/00Personal AutoFour-Door Car1131.464935
2AI49188NevadaFBachelor1288743.1748767.0108.01/0/00Personal AutoTwo-Door Car566.472247
3WW63253CaliforniaMBachelor764586.180.0106.01/0/00Corporate AutoSUV529.881344
4GA49547WashingtonMHigh School or Below536307.6536357.068.01/0/00Personal AutoFour-Door Car17.269323
....................................
4003NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4004NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4005NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4006NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4007NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
\n", + "

4008 rows × 11 columns

\n", + "
" + ], + "text/plain": [ + " customer state gender education \\\n", + "0 RB50392 Washington NaN Master \n", + "1 QZ44356 Arizona F Bachelor \n", + "2 AI49188 Nevada F Bachelor \n", + "3 WW63253 California M Bachelor \n", + "4 GA49547 Washington M High School or Below \n", + "... ... ... ... ... \n", + "4003 NaN NaN NaN NaN \n", + "4004 NaN NaN NaN NaN \n", + "4005 NaN NaN NaN NaN \n", + "4006 NaN NaN NaN NaN \n", + "4007 NaN NaN NaN NaN \n", + "\n", + " customer_lifetime_value income monthly_premium_auto \\\n", + "0 NaN 0.0 1000.0 \n", + "1 697953.59 0.0 94.0 \n", + "2 1288743.17 48767.0 108.0 \n", + "3 764586.18 0.0 106.0 \n", + "4 536307.65 36357.0 68.0 \n", + "... ... ... ... \n", + "4003 NaN NaN NaN \n", + "4004 NaN NaN NaN \n", + "4005 NaN NaN NaN \n", + "4006 NaN NaN NaN \n", + "4007 NaN NaN NaN \n", + "\n", + " number_of_open_complaints Policy Type vehicle_class \\\n", + "0 1/0/00 Personal Auto Four-Door Car \n", + "1 1/0/00 Personal Auto Four-Door Car \n", + "2 1/0/00 Personal Auto Two-Door Car \n", + "3 1/0/00 Corporate Auto SUV \n", + "4 1/0/00 Personal Auto Four-Door Car \n", + "... ... ... ... \n", + "4003 NaN NaN NaN \n", + "4004 NaN NaN NaN \n", + "4005 NaN NaN NaN \n", + "4006 NaN NaN NaN \n", + "4007 NaN NaN NaN \n", + "\n", + " total_claim_amount \n", + "0 2.704934 \n", + "1 1131.464935 \n", + "2 566.472247 \n", + "3 529.881344 \n", + "4 17.269323 \n", + "... ... \n", + "4003 NaN \n", + "4004 NaN \n", + "4005 NaN \n", + "4006 NaN \n", + "4007 NaN \n", + "\n", + "[4008 rows x 11 columns]" ] - }, + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "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": 85, + "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9", + "metadata": { + "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9" + }, + "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": [ + "customer object\n", + "state object\n", + "gender object\n", + "education object\n", + "customer_lifetime_value object\n", + "income float64\n", + "monthly_premium_auto float64\n", + "number_of_open_complaints object\n", + "Policy Type object\n", + "vehicle_class object\n", + "total_claim_amount float64\n", + "dtype: object" ] - }, + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.dtypes" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "fa97cc0d-79ff-4c04-a3db-3e2520d91090", + "metadata": {}, + "outputs": [], + "source": [ + "df['customer_lifetime_value'] = df['customer_lifetime_value'].astype(float)" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "34946121-c622-4716-9c5a-9ad165b1ca14", + "metadata": {}, + "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." + "data": { + "text/plain": [ + "customer object\n", + "state object\n", + "gender object\n", + "education object\n", + "customer_lifetime_value float64\n", + "income float64\n", + "monthly_premium_auto float64\n", + "number_of_open_complaints object\n", + "Policy Type object\n", + "vehicle_class object\n", + "total_claim_amount float64\n", + "dtype: object" ] - }, + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.dtypes" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "41cc3797-9691-409d-843a-d4ffdf23ef95", + "metadata": {}, + "outputs": [], + "source": [ + "#I do not know how to do the last part" + ] + }, + { + "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": 91, + "id": "05f0740b-77aa-4edc-b08a-871ec08d941e", + "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/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", + " \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.590.094.01/0/00Personal AutoFour-Door Car1131.464935
2AI49188NevadaFBachelor1288743.1748767.0108.01/0/00Personal AutoTwo-Door Car566.472247
3WW63253CaliforniaMBachelor764586.180.0106.01/0/00Corporate AutoSUV529.881344
4GA49547WashingtonMHigh School or Below536307.6536357.068.01/0/00Personal AutoFour-Door Car17.269323
....................................
4003NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4004NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4005NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4006NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
4007NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
\n", + "

4008 rows × 11 columns

\n", + "
" + ], + "text/plain": [ + " customer state gender education \\\n", + "0 RB50392 Washington NaN Master \n", + "1 QZ44356 Arizona F Bachelor \n", + "2 AI49188 Nevada F Bachelor \n", + "3 WW63253 California M Bachelor \n", + "4 GA49547 Washington M High School or Below \n", + "... ... ... ... ... \n", + "4003 NaN NaN NaN NaN \n", + "4004 NaN NaN NaN NaN \n", + "4005 NaN NaN NaN NaN \n", + "4006 NaN NaN NaN NaN \n", + "4007 NaN NaN NaN NaN \n", + "\n", + " customer_lifetime_value income monthly_premium_auto \\\n", + "0 NaN 0.0 1000.0 \n", + "1 697953.59 0.0 94.0 \n", + "2 1288743.17 48767.0 108.0 \n", + "3 764586.18 0.0 106.0 \n", + "4 536307.65 36357.0 68.0 \n", + "... ... ... ... \n", + "4003 NaN NaN NaN \n", + "4004 NaN NaN NaN \n", + "4005 NaN NaN NaN \n", + "4006 NaN NaN NaN \n", + "4007 NaN NaN NaN \n", + "\n", + " number_of_open_complaints Policy Type vehicle_class \\\n", + "0 1/0/00 Personal Auto Four-Door Car \n", + "1 1/0/00 Personal Auto Four-Door Car \n", + "2 1/0/00 Personal Auto Two-Door Car \n", + "3 1/0/00 Corporate Auto SUV \n", + "4 1/0/00 Personal Auto Four-Door Car \n", + "... ... ... ... \n", + "4003 NaN NaN NaN \n", + "4004 NaN NaN NaN \n", + "4005 NaN NaN NaN \n", + "4006 NaN NaN NaN \n", + "4007 NaN NaN NaN \n", + "\n", + " total_claim_amount \n", + "0 2.704934 \n", + "1 1131.464935 \n", + "2 566.472247 \n", + "3 529.881344 \n", + "4 17.269323 \n", + "... ... \n", + "4003 NaN \n", + "4004 NaN \n", + "4005 NaN \n", + "4006 NaN \n", + "4007 NaN \n", + "\n", + "[4008 rows x 11 columns]" ] - }, + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e", + "metadata": { + "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e" + }, + "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/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", + " \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
0FalseFalseTrueFalseTrueFalseFalseFalseFalseFalseFalse
1FalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
2FalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
3FalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
4FalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
....................................
4003TrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
4004TrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
4005TrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
4006TrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
4007TrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
\n", + "

4008 rows × 11 columns

\n", + "
" + ], + "text/plain": [ + " customer state gender education customer_lifetime_value income \\\n", + "0 False False True False True False \n", + "1 False False False False False False \n", + "2 False False False False False False \n", + "3 False False False False False False \n", + "4 False False False False False False \n", + "... ... ... ... ... ... ... \n", + "4003 True True True True True True \n", + "4004 True True True True True True \n", + "4005 True True True True True True \n", + "4006 True True True True True True \n", + "4007 True True True True True True \n", + "\n", + " monthly_premium_auto number_of_open_complaints Policy Type \\\n", + "0 False False False \n", + "1 False False False \n", + "2 False False False \n", + "3 False False False \n", + "4 False False False \n", + "... ... ... ... \n", + "4003 True True True \n", + "4004 True True True \n", + "4005 True True True \n", + "4006 True True True \n", + "4007 True True True \n", + "\n", + " vehicle_class total_claim_amount \n", + "0 False False \n", + "1 False False \n", + "2 False False \n", + "3 False False \n", + "4 False False \n", + "... ... ... \n", + "4003 True True \n", + "4004 True True \n", + "4005 True True \n", + "4006 True True \n", + "4007 True True \n", + "\n", + "[4008 rows x 11 columns]" ] - }, + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isna()" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "18183355-cd5a-4762-a53e-17fb2c10962d", + "metadata": {}, + "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": [ + "customer 2937\n", + "state 2937\n", + "gender 3054\n", + "education 2937\n", + "customer_lifetime_value 2940\n", + "income 2937\n", + "monthly_premium_auto 2937\n", + "number_of_open_complaints 2937\n", + "Policy Type 2937\n", + "vehicle_class 2937\n", + "total_claim_amount 2937\n", + "dtype: int64" ] - }, + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isna().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "88e88a5b-f3f2-4707-acc5-fc775aaf66ca", + "metadata": {}, + "outputs": [], + "source": [ + "df = df.dropna()" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "id": "a7ec41fa-7fd3-4310-9bbc-83adfa907e85", + "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." + "data": { + "text/plain": [ + "customer 0\n", + "state 0\n", + "gender 0\n", + "education 0\n", + "customer_lifetime_value 0\n", + "income 0\n", + "monthly_premium_auto 0\n", + "number_of_open_complaints 0\n", + "Policy Type 0\n", + "vehicle_class 0\n", + "total_claim_amount 0\n", + "dtype: int64" ] - }, + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isna().sum()" + ] + }, + { + "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": 119, + "id": "1929362c-47ed-47cb-baca-358b78d401a0", + "metadata": { + "id": "1929362c-47ed-47cb-baca-358b78d401a0" + }, + "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" + "data": { + "text/plain": [ + "0" ] + }, + "execution_count": 119, + "metadata": {}, + "output_type": "execute_result" } - ], - "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.duplicated().sum()" + ] + }, + { + "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": 41, + "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": 44, + "id": "211e82b5-461a-4d6f-8a23-4deccb84173c", + "metadata": { + "id": "211e82b5-461a-4d6f-8a23-4deccb84173c" + }, + "outputs": [], + "source": [ + "# Your code here" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 }