diff --git a/02_lab-dw-data-cleaning-and-formatting.ipynb b/02_lab-dw-data-cleaning-and-formatting.ipynb
new file mode 100644
index 0000000..32a1779
--- /dev/null
+++ b/02_lab-dw-data-cleaning-and-formatting.ipynb
@@ -0,0 +1,2047 @@
+{
+ "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": [
+ {
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Customer | \n",
+ " ST | \n",
+ " GENDER | \n",
+ " Education | \n",
+ " Customer Lifetime Value | \n",
+ " Income | \n",
+ " Monthly Premium Auto | \n",
+ " Number of Open Complaints | \n",
+ " Policy Type | \n",
+ " Vehicle Class | \n",
+ " Total Claim Amount | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " RB50392 | \n",
+ " Washington | \n",
+ " NaN | \n",
+ " Master | \n",
+ " NaN | \n",
+ " 0.0 | \n",
+ " 1000.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 2.704934 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " QZ44356 | \n",
+ " Arizona | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 697953.59% | \n",
+ " 0.0 | \n",
+ " 94.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 1131.464935 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " AI49188 | \n",
+ " Nevada | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 1288743.17% | \n",
+ " 48767.0 | \n",
+ " 108.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Two-Door Car | \n",
+ " 566.472247 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " WW63253 | \n",
+ " California | \n",
+ " M | \n",
+ " Bachelor | \n",
+ " 764586.18% | \n",
+ " 0.0 | \n",
+ " 106.0 | \n",
+ " 1/0/00 | \n",
+ " Corporate Auto | \n",
+ " SUV | \n",
+ " 529.881344 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " GA49547 | \n",
+ " Washington | \n",
+ " M | \n",
+ " High School or Below | \n",
+ " 536307.65% | \n",
+ " 36357.0 | \n",
+ " 68.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 17.269323 | \n",
+ "
\n",
+ " \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " 4003 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " 4004 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " 4005 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " 4006 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " 4007 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ "
\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": 1,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "import pandas as pd\n",
+ "import re\n",
+ "\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": 3,
+ "id": "67372727-02ad-46a8-b2b8-5bd99ad3ec0b",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Original column names:\n",
+ "['Customer', 'ST', 'GENDER', 'Education', 'Customer Lifetime Value', 'Income', 'Monthly Premium Auto', 'Number of Open Complaints', 'Policy Type', 'Vehicle Class', 'Total Claim Amount']\n",
+ "\n",
+ " Cleaned column names:\n"
+ ]
+ },
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " customer | \n",
+ " st | \n",
+ " gender | \n",
+ " education | \n",
+ " customer_lifetime_value | \n",
+ " income | \n",
+ " monthly_premium_auto | \n",
+ " number_of_open_complaints | \n",
+ " policy_type | \n",
+ " vehicle_class | \n",
+ " total_claim_amount | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " RB50392 | \n",
+ " Washington | \n",
+ " NaN | \n",
+ " Master | \n",
+ " NaN | \n",
+ " 0.0 | \n",
+ " 1000.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 2.704934 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " QZ44356 | \n",
+ " Arizona | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 697953.59% | \n",
+ " 0.0 | \n",
+ " 94.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 1131.464935 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " AI49188 | \n",
+ " Nevada | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 1288743.17% | \n",
+ " 48767.0 | \n",
+ " 108.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Two-Door Car | \n",
+ " 566.472247 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " WW63253 | \n",
+ " California | \n",
+ " M | \n",
+ " Bachelor | \n",
+ " 764586.18% | \n",
+ " 0.0 | \n",
+ " 106.0 | \n",
+ " 1/0/00 | \n",
+ " Corporate Auto | \n",
+ " SUV | \n",
+ " 529.881344 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " GA49547 | \n",
+ " Washington | \n",
+ " M | \n",
+ " High School or Below | \n",
+ " 536307.65% | \n",
+ " 36357.0 | \n",
+ " 68.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 17.269323 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " 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 "
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "print(\"Original column names:\")\n",
+ "print(df.columns.tolist())\n",
+ "\n",
+ "df.columns = df.columns.str.lower() #Convert to lowercase #\n",
+ "df.columns = df.columns.str.replace(' ', '_') # Replace spaces with underscores\n",
+ "df.rename(columns={'state': 'st'}, inplace=True)\n",
+ "\n",
+ "print(\"\\n Cleaned column names:\")\n",
+ "# print(df.columns.tolist())\n",
+ "\n",
+ "df.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "b932d0e8-4773-41db-902f-c036ac56ac2e",
+ "metadata": {},
+ "outputs": [
+ {
+ "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"
+ ]
+ }
+ ],
+ "source": [
+ "df.info()"
+ ]
+ },
+ {
+ "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": 15,
+ "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c",
+ "metadata": {
+ "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array(['female', 'Male', 'M', 'Femal', 'F', nan], dtype=object)"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "\n",
+ "#regex_gender = r'^(f(emale)?|m(ale)?)$'\n",
+ "\n",
+ "#df['gender'] = df['gender'].str.replace(regex_gender, {'F','M'}, regex=True)\n",
+ "\n",
+ "df['gender'].sort_values(ascending = False).unique()\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "id": "7f650b10-fefc-437b-b029-5b7631546610",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def standardize_gender(gender):\n",
+ " if pd.isnull(gender):\n",
+ " return None\n",
+ " gender = gender.strip().lower() \n",
+ " if gender in [ 'f', 'female', 'Femal']:\n",
+ " return 'F'\n",
+ " elif gender in ['m', 'Male']:\n",
+ " return 'M'\n",
+ " else:\n",
+ " return None \n",
+ "\n",
+ "\n",
+ "df['gender'] = df['gender'].apply(standardize_gender)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "id": "e14f4d09-aea1-44ae-8885-234eac8b7cf4",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array(['M', 'F', None], dtype=object)"
+ ]
+ },
+ "execution_count": 21,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df['gender'].sort_values(ascending = False).unique()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "id": "f4563702-d7c2-4a45-81e2-1365d2289002",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " customer | \n",
+ " st | \n",
+ " gender | \n",
+ " education | \n",
+ " customer_lifetime_value | \n",
+ " income | \n",
+ " monthly_premium_auto | \n",
+ " number_of_open_complaints | \n",
+ " policy_type | \n",
+ " vehicle_class | \n",
+ " total_claim_amount | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " RB50392 | \n",
+ " Washington | \n",
+ " None | \n",
+ " Master | \n",
+ " NaN | \n",
+ " 0.0 | \n",
+ " 1000.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 2.704934 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " QZ44356 | \n",
+ " Arizona | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 697953.59% | \n",
+ " 0.0 | \n",
+ " 94.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 1131.464935 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " AI49188 | \n",
+ " Nevada | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 1288743.17% | \n",
+ " 48767.0 | \n",
+ " 108.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Two-Door Car | \n",
+ " 566.472247 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " WW63253 | \n",
+ " California | \n",
+ " M | \n",
+ " Bachelor | \n",
+ " 764586.18% | \n",
+ " 0.0 | \n",
+ " 106.0 | \n",
+ " 1/0/00 | \n",
+ " Corporate Auto | \n",
+ " SUV | \n",
+ " 529.881344 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " GA49547 | \n",
+ " Washington | \n",
+ " M | \n",
+ " High School or Below | \n",
+ " 536307.65% | \n",
+ " 36357.0 | \n",
+ " 68.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 17.269323 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " customer st gender education customer_lifetime_value \\\n",
+ "0 RB50392 Washington None 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": 23,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.loc[df['st'] == 'Cali', 'st'] = 'California' \n",
+ "df.loc[df['st'] == 'WA', 'st'] = 'Washington'\n",
+ "df.loc[df['st'] == 'AZ', 'st'] = 'Arizona'\n",
+ "\n",
+ "df.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "id": "148f0700-8155-4c49-8e33-8f0e39073a08",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array(['Arizona', 'California', 'Nevada', 'Oregon', 'Washington', nan],\n",
+ " dtype=object)"
+ ]
+ },
+ "execution_count": 25,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df['st'].sort_values(ascending = True).unique()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "id": "7b1c37a3-7086-4461-b399-dea17b533dc3",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array(['Bachelor', 'College', 'Doctor', 'High School or Below', 'Master',\n",
+ " nan], dtype=object)"
+ ]
+ },
+ "execution_count": 27,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.loc[df['education'] == 'Bachelors', 'education'] = \"Bachelor\"\n",
+ "df['education'].sort_values(ascending = True).unique()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 29,
+ "id": "7f2c0015-aaea-4d29-903e-c124f5f44105",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " customer | \n",
+ " st | \n",
+ " gender | \n",
+ " education | \n",
+ " customer_lifetime_value | \n",
+ " income | \n",
+ " monthly_premium_auto | \n",
+ " number_of_open_complaints | \n",
+ " policy_type | \n",
+ " vehicle_class | \n",
+ " total_claim_amount | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " RB50392 | \n",
+ " Washington | \n",
+ " None | \n",
+ " Master | \n",
+ " NaN | \n",
+ " 0.0 | \n",
+ " 1000.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 2.704934 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " QZ44356 | \n",
+ " Arizona | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 697953.59 | \n",
+ " 0.0 | \n",
+ " 94.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 1131.464935 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " AI49188 | \n",
+ " Nevada | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 1288743.17 | \n",
+ " 48767.0 | \n",
+ " 108.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Two-Door Car | \n",
+ " 566.472247 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " WW63253 | \n",
+ " California | \n",
+ " M | \n",
+ " Bachelor | \n",
+ " 764586.18 | \n",
+ " 0.0 | \n",
+ " 106.0 | \n",
+ " 1/0/00 | \n",
+ " Corporate Auto | \n",
+ " SUV | \n",
+ " 529.881344 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " GA49547 | \n",
+ " Washington | \n",
+ " M | \n",
+ " High School or Below | \n",
+ " 536307.65 | \n",
+ " 36357.0 | \n",
+ " 68.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 17.269323 | \n",
+ "
\n",
+ " \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " 4003 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " None | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " 4004 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " None | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " 4005 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " None | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " 4006 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " None | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " 4007 | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " None | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
4008 rows × 11 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " customer st gender education \\\n",
+ "0 RB50392 Washington None 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 None NaN \n",
+ "4004 NaN NaN None NaN \n",
+ "4005 NaN NaN None NaN \n",
+ "4006 NaN NaN None NaN \n",
+ "4007 NaN NaN None 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": 29,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "regex_customer = r'(?<=\\w)%'\n",
+ "df['customer_lifetime_value'] = df['customer_lifetime_value'].str.replace(regex_customer, '', regex=True)\n",
+ "df"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 31,
+ "id": "595e7078-141a-4d94-b8c4-70d7a53d84fe",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array(['Four-Door Car', 'Luxury', 'SUV', 'Two-Door Car', nan],\n",
+ " dtype=object)"
+ ]
+ },
+ "execution_count": 31,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "#In vehicle class, \"Sports Car\", \"Luxury SUV\" and \"Luxury Car\" could be replaced by \"Luxury\"\n",
+ " \n",
+ "df['vehicle_class'] = df['vehicle_class'].replace(r'(Sports Car|Luxury SUV|Luxury Car)', 'Luxury', regex=True)\n",
+ "\n",
+ "df['vehicle_class'].sort_values(ascending = True).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": 33,
+ "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9",
+ "metadata": {
+ "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([ 200435.07, 205062.35, 206445.88, ..., 4479546.94, 5816655.35,\n",
+ " nan])"
+ ]
+ },
+ "execution_count": 33,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df['customer_lifetime_value'] = pd.to_numeric(df['customer_lifetime_value'], errors='coerce')\n",
+ "\n",
+ "\"\"\"\n",
+ " errors='coerce' parameter ensures that any invalid values will be converted to NaN\n",
+ " \n",
+ "\"\"\"\n",
+ "df['customer_lifetime_value'].sort_values(ascending = True).unique()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 35,
+ "id": "50c7e486-e878-418b-9152-de653a326df1",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# 2. Fix 'Number of Open Complaints' by taking the middle value and converting to numeric\n",
+ "\n",
+ "# df['number_of_open_complaints'] = df['number_of_open_complaints'].astype(str).str.split('/').str[1]\n",
+ "# df['number_of_open_complaints'] = pd.to_numeric(df['number_of_open_complaints'], errors='coerce')\n",
+ "# df['number_of_open_complaints']\n",
+ "df['number_of_open_complaints'].unique()\n",
+ "\n",
+ "df.loc[df['number_of_open_complaints'] == '1/0/00', \n",
+ "'number_of_open_complaints'] = \"0\"\n",
+ "\n",
+ "df.loc[df['number_of_open_complaints'] == '1/1/00', \n",
+ "'number_of_open_complaints'] = \"1\" \n",
+ "\n",
+ "df.loc[df['number_of_open_complaints'] == '1/2/00', \n",
+ "'number_of_open_complaints'] = \"2\" \n",
+ "\n",
+ "df.loc[df['number_of_open_complaints'] == '1/3/00', \n",
+ "'number_of_open_complaints'] = \"3\" \n",
+ "\n",
+ "df.loc[df['number_of_open_complaints'] == '1/4/00', \n",
+ "'number_of_open_complaints'] = \"4\" \n",
+ "\n",
+ "df.loc[df['number_of_open_complaints'] == '1/5/00', \n",
+ "'number_of_open_complaints'] = \"5\" "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "id": "67232a54-2b2a-4789-b44b-53c45ae845db",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array(['0', '2', '1', '3', '5', '4', nan], dtype=object)"
+ ]
+ },
+ "execution_count": 37,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df['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": 39,
+ "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e",
+ "metadata": {
+ "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "customer 2937\n",
+ "st 2937\n",
+ "gender 3110\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\n"
+ ]
+ }
+ ],
+ "source": [
+ "null_counts = df.isnull().sum()\n",
+ "print(null_counts[null_counts > 0]) "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 41,
+ "id": "ce33ccc3-cbd9-4427-bce8-362294015d86",
+ "metadata": {},
+ "outputs": [
+ {
+ "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 898 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.info()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 73,
+ "id": "01893a22-da96-4711-90e0-4b4ab791a1cd",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " customer | \n",
+ " st | \n",
+ " gender | \n",
+ " education | \n",
+ " customer_lifetime_value | \n",
+ " income | \n",
+ " monthly_premium_auto | \n",
+ " number_of_open_complaints | \n",
+ " policy_type | \n",
+ " vehicle_class | \n",
+ " total_claim_amount | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " RB50392 | \n",
+ " Washington | \n",
+ " None | \n",
+ " Master | \n",
+ " NaN | \n",
+ " 0.0 | \n",
+ " 1000.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 2.704934 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " QZ44356 | \n",
+ " Arizona | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 697953.59 | \n",
+ " 0.0 | \n",
+ " 94.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 1131.464935 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " AI49188 | \n",
+ " Nevada | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 1288743.17 | \n",
+ " 48767.0 | \n",
+ " 108.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Two-Door Car | \n",
+ " 566.472247 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " WW63253 | \n",
+ " California | \n",
+ " M | \n",
+ " Bachelor | \n",
+ " 764586.18 | \n",
+ " 0.0 | \n",
+ " 106.0 | \n",
+ " 0 | \n",
+ " Corporate Auto | \n",
+ " SUV | \n",
+ " 529.881344 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " GA49547 | \n",
+ " Washington | \n",
+ " M | \n",
+ " High School or Below | \n",
+ " 536307.65 | \n",
+ " 36357.0 | \n",
+ " 68.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 17.269323 | \n",
+ "
\n",
+ " \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " 1066 | \n",
+ " TM65736 | \n",
+ " Oregon | \n",
+ " M | \n",
+ " Master | \n",
+ " 305955.03 | \n",
+ " 38644.0 | \n",
+ " 78.0 | \n",
+ " 1 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 361.455219 | \n",
+ "
\n",
+ " \n",
+ " 1067 | \n",
+ " VJ51327 | \n",
+ " California | \n",
+ " F | \n",
+ " High School or Below | \n",
+ " 2031499.76 | \n",
+ " 63209.0 | \n",
+ " 102.0 | \n",
+ " 2 | \n",
+ " Personal Auto | \n",
+ " SUV | \n",
+ " 207.320041 | \n",
+ "
\n",
+ " \n",
+ " 1068 | \n",
+ " GS98873 | \n",
+ " Arizona | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 323912.47 | \n",
+ " 16061.0 | \n",
+ " 88.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 633.600000 | \n",
+ "
\n",
+ " \n",
+ " 1069 | \n",
+ " CW49887 | \n",
+ " California | \n",
+ " F | \n",
+ " Master | \n",
+ " 462680.11 | \n",
+ " 79487.0 | \n",
+ " 114.0 | \n",
+ " 0 | \n",
+ " Special Auto | \n",
+ " SUV | \n",
+ " 547.200000 | \n",
+ "
\n",
+ " \n",
+ " 1070 | \n",
+ " MY31220 | \n",
+ " California | \n",
+ " F | \n",
+ " College | \n",
+ " 899704.02 | \n",
+ " 54230.0 | \n",
+ " 112.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Two-Door Car | \n",
+ " 537.600000 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
1071 rows × 11 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " customer st gender education \\\n",
+ "0 RB50392 Washington None 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",
+ "1066 TM65736 Oregon M Master \n",
+ "1067 VJ51327 California F High School or Below \n",
+ "1068 GS98873 Arizona F Bachelor \n",
+ "1069 CW49887 California F Master \n",
+ "1070 MY31220 California F College \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",
+ "1066 305955.03 38644.0 78.0 \n",
+ "1067 2031499.76 63209.0 102.0 \n",
+ "1068 323912.47 16061.0 88.0 \n",
+ "1069 462680.11 79487.0 114.0 \n",
+ "1070 899704.02 54230.0 112.0 \n",
+ "\n",
+ " number_of_open_complaints policy_type vehicle_class \\\n",
+ "0 0 Personal Auto Four-Door Car \n",
+ "1 0 Personal Auto Four-Door Car \n",
+ "2 0 Personal Auto Two-Door Car \n",
+ "3 0 Corporate Auto SUV \n",
+ "4 0 Personal Auto Four-Door Car \n",
+ "... ... ... ... \n",
+ "1066 1 Personal Auto Four-Door Car \n",
+ "1067 2 Personal Auto SUV \n",
+ "1068 0 Personal Auto Four-Door Car \n",
+ "1069 0 Special Auto SUV \n",
+ "1070 0 Personal Auto Two-Door Car \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",
+ "1066 361.455219 \n",
+ "1067 207.320041 \n",
+ "1068 633.600000 \n",
+ "1069 547.200000 \n",
+ "1070 537.600000 \n",
+ "\n",
+ "[1071 rows x 11 columns]"
+ ]
+ },
+ "execution_count": 73,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.dropna(axis = 'index', how = 'all', inplace=True)\n",
+ "\"\"\"\n",
+ "Strategy:\n",
+ "Dropping Rows with All Null Values - discarding these rows does not significantly reduce the dataset's integrity.\n",
+ "Columns with Null Values are preserved, since the specific scope of analysis is not yet determend. This maintains dataset \n",
+ "simplicity without introducing potential bias from imputation. \n",
+ "\n",
+ "\"\"\"\n",
+ "df"
+ ]
+ },
+ {
+ "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": 95,
+ "id": "1929362c-47ed-47cb-baca-358b78d401a0",
+ "metadata": {
+ "id": "1929362c-47ed-47cb-baca-358b78d401a0"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " customer | \n",
+ " st | \n",
+ " gender | \n",
+ " education | \n",
+ " customer_lifetime_value | \n",
+ " income | \n",
+ " monthly_premium_auto | \n",
+ " number_of_open_complaints | \n",
+ " policy_type | \n",
+ " vehicle_class | \n",
+ " total_claim_amount | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " RB50392 | \n",
+ " Washington | \n",
+ " None | \n",
+ " Master | \n",
+ " NaN | \n",
+ " 0.0 | \n",
+ " 1000.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 2.704934 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " QZ44356 | \n",
+ " Arizona | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 697953.59 | \n",
+ " 0.0 | \n",
+ " 94.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 1131.464935 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " AI49188 | \n",
+ " Nevada | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 1288743.17 | \n",
+ " 48767.0 | \n",
+ " 108.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Two-Door Car | \n",
+ " 566.472247 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " WW63253 | \n",
+ " California | \n",
+ " M | \n",
+ " Bachelor | \n",
+ " 764586.18 | \n",
+ " 0.0 | \n",
+ " 106.0 | \n",
+ " 0 | \n",
+ " Corporate Auto | \n",
+ " SUV | \n",
+ " 529.881344 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " GA49547 | \n",
+ " Washington | \n",
+ " M | \n",
+ " High School or Below | \n",
+ " 536307.65 | \n",
+ " 36357.0 | \n",
+ " 68.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 17.269323 | \n",
+ "
\n",
+ " \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " 1066 | \n",
+ " TM65736 | \n",
+ " Oregon | \n",
+ " M | \n",
+ " Master | \n",
+ " 305955.03 | \n",
+ " 38644.0 | \n",
+ " 78.0 | \n",
+ " 1 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 361.455219 | \n",
+ "
\n",
+ " \n",
+ " 1067 | \n",
+ " VJ51327 | \n",
+ " California | \n",
+ " F | \n",
+ " High School or Below | \n",
+ " 2031499.76 | \n",
+ " 63209.0 | \n",
+ " 102.0 | \n",
+ " 2 | \n",
+ " Personal Auto | \n",
+ " SUV | \n",
+ " 207.320041 | \n",
+ "
\n",
+ " \n",
+ " 1068 | \n",
+ " GS98873 | \n",
+ " Arizona | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 323912.47 | \n",
+ " 16061.0 | \n",
+ " 88.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 633.600000 | \n",
+ "
\n",
+ " \n",
+ " 1069 | \n",
+ " CW49887 | \n",
+ " California | \n",
+ " F | \n",
+ " Master | \n",
+ " 462680.11 | \n",
+ " 79487.0 | \n",
+ " 114.0 | \n",
+ " 0 | \n",
+ " Special Auto | \n",
+ " SUV | \n",
+ " 547.200000 | \n",
+ "
\n",
+ " \n",
+ " 1070 | \n",
+ " MY31220 | \n",
+ " California | \n",
+ " F | \n",
+ " College | \n",
+ " 899704.02 | \n",
+ " 54230.0 | \n",
+ " 112.0 | \n",
+ " 0 | \n",
+ " Personal Auto | \n",
+ " Two-Door Car | \n",
+ " 537.600000 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
1071 rows × 11 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " customer st gender education \\\n",
+ "0 RB50392 Washington None 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",
+ "1066 TM65736 Oregon M Master \n",
+ "1067 VJ51327 California F High School or Below \n",
+ "1068 GS98873 Arizona F Bachelor \n",
+ "1069 CW49887 California F Master \n",
+ "1070 MY31220 California F College \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",
+ "1066 305955.03 38644.0 78.0 \n",
+ "1067 2031499.76 63209.0 102.0 \n",
+ "1068 323912.47 16061.0 88.0 \n",
+ "1069 462680.11 79487.0 114.0 \n",
+ "1070 899704.02 54230.0 112.0 \n",
+ "\n",
+ " number_of_open_complaints policy_type vehicle_class \\\n",
+ "0 0 Personal Auto Four-Door Car \n",
+ "1 0 Personal Auto Four-Door Car \n",
+ "2 0 Personal Auto Two-Door Car \n",
+ "3 0 Corporate Auto SUV \n",
+ "4 0 Personal Auto Four-Door Car \n",
+ "... ... ... ... \n",
+ "1066 1 Personal Auto Four-Door Car \n",
+ "1067 2 Personal Auto SUV \n",
+ "1068 0 Personal Auto Four-Door Car \n",
+ "1069 0 Special Auto SUV \n",
+ "1070 0 Personal Auto Two-Door Car \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",
+ "1066 361.455219 \n",
+ "1067 207.320041 \n",
+ "1068 633.600000 \n",
+ "1069 547.200000 \n",
+ "1070 537.600000 \n",
+ "\n",
+ "[1071 rows x 11 columns]"
+ ]
+ },
+ "execution_count": 95,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.duplicated(subset=None, keep='first')\n",
+ "df"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 102,
+ "id": "ad6b15c5-3ac3-4970-9998-97c7ad7102f7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "df.to_csv('/Users/air/ironHack/Course/pandas/datasets/cleaned_data_lab02.csv', index=False)\n"
+ ]
+ },
+ {
+ "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": null,
+ "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": null,
+ "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"
+ },
+ "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.4"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}