diff --git a/lab-dw-data-cleaning-and-formatting.ipynb b/lab-dw-data-cleaning-and-formatting.ipynb index cdfc3c6..2e7e619 100644 --- a/lab-dw-data-cleaning-and-formatting.ipynb +++ b/lab-dw-data-cleaning-and-formatting.ipynb @@ -1,371 +1,594 @@ { - "cells": [ - { - "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", - "metadata": { - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e" - }, - "source": [ - "# Lab | Data Cleaning and Formatting" - ] - }, - { - "cell_type": "markdown", - "id": "d1973e9e-8be6-4039-b70e-d73ee0d94c99", - "metadata": { - "id": "d1973e9e-8be6-4039-b70e-d73ee0d94c99" - }, - "source": [ - "In this lab, we will be working with the customer data from an insurance company, which can be found in the CSV file located at the following link: https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file1.csv\n" - ] - }, - { - "cell_type": "markdown", - "id": "31b8a9e7-7db9-4604-991b-ef6771603e57", - "metadata": { - "id": "31b8a9e7-7db9-4604-991b-ef6771603e57" - }, - "source": [ - "# Challenge 1: Data Cleaning and Formatting" - ] - }, - { - "cell_type": "markdown", - "id": "81553f19-9f2c-484b-8940-520aff884022", - "metadata": { - "id": "81553f19-9f2c-484b-8940-520aff884022" - }, - "source": [ - "## Exercise 1: Cleaning Column Names" - ] - }, - { - "cell_type": "markdown", - "id": "34a929f4-1be4-4fa8-adda-42ffd920be90", - "metadata": { - "id": "34a929f4-1be4-4fa8-adda-42ffd920be90" - }, - "source": [ - "To ensure consistency and ease of use, standardize the column names of the dataframe. Start by taking a first look at the dataframe and identifying any column names that need to be modified. Use appropriate naming conventions and make sure that column names are descriptive and informative.\n", - "\n", - "*Hint*:\n", - "- *Column names should be in lower case*\n", - "- *White spaces in column names should be replaced by `_`*\n", - "- *`st` could be replaced for `state`*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5810735c-8056-4442-bbf2-dda38d3e284a", - "metadata": { - "id": "5810735c-8056-4442-bbf2-dda38d3e284a" - }, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "id": "9cb501ec-36ff-4589-b872-6252bb150316", - "metadata": { - "id": "9cb501ec-36ff-4589-b872-6252bb150316" - }, - "source": [ - "## Exercise 2: Cleaning invalid Values" - ] - }, - { - "cell_type": "markdown", - "id": "771fdcf3-8e20-4b06-9c24-3a93ba2b0909", - "metadata": { - "id": "771fdcf3-8e20-4b06-9c24-3a93ba2b0909" - }, - "source": [ - "The dataset contains columns with inconsistent and incorrect values that could affect the accuracy of our analysis. Therefore, we need to clean these columns to ensure that they only contain valid data.\n", - "\n", - "Note that this exercise will focus only on cleaning inconsistent values and will not involve handling null values (NaN or None).\n", - "\n", - "*Hint*:\n", - "- *Gender column contains various inconsistent values such as \"F\", \"M\", \"Femal\", \"Male\", \"female\", which need to be standardized, for example, to \"M\" and \"F\".*\n", - "- *State abbreviations be can replaced with its full name, for example \"AZ\": \"Arizona\", \"Cali\": \"California\", \"WA\": \"Washington\"*\n", - "- *In education, \"Bachelors\" could be replaced by \"Bachelor\"*\n", - "- *In Customer Lifetime Value, delete the `%` character*\n", - "- *In vehicle class, \"Sports Car\", \"Luxury SUV\" and \"Luxury Car\" could be replaced by \"Luxury\"*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c", - "metadata": { - "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c" - }, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "id": "85ff78ce-0174-4890-9db3-8048b7d7d2d0", - "metadata": { - "id": "85ff78ce-0174-4890-9db3-8048b7d7d2d0" - }, - "source": [ - "## Exercise 3: Formatting data types" - ] - }, - { - "cell_type": "markdown", - "id": "b91c2cf8-79a2-4baf-9f65-ff2fb22270bd", - "metadata": { - "id": "b91c2cf8-79a2-4baf-9f65-ff2fb22270bd" - }, - "source": [ - "The data types of many columns in the dataset appear to be incorrect. This could impact the accuracy of our analysis. To ensure accurate analysis, we need to correct the data types of these columns. Please update the data types of the columns as appropriate." - ] - }, - { - "cell_type": "markdown", - "id": "43e5d853-ff9e-43b2-9d92-aef2f78764f3", - "metadata": { - "id": "43e5d853-ff9e-43b2-9d92-aef2f78764f3" - }, - "source": [ - "It is important to note that this exercise does not involve handling null values (NaN or None)." - ] - }, - { - "cell_type": "markdown", - "id": "329ca691-9196-4419-8969-3596746237a1", - "metadata": { - "id": "329ca691-9196-4419-8969-3596746237a1" - }, - "source": [ - "*Hint*:\n", - "- *Customer lifetime value should be numeric*\n", - "- *Number of open complaints has an incorrect format. Look at the different values it takes with `unique()` and take the middle value. As an example, 1/5/00 should be 5. Number of open complaints is a string - remember you can use `split()` to deal with it and take the number you need. Finally, since it should be numeric, cast the column to be in its proper type.*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9", - "metadata": { - "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9" - }, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "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": null, - "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e", - "metadata": { - "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e" - }, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "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": null, - "id": "1929362c-47ed-47cb-baca-358b78d401a0", - "metadata": { - "id": "1929362c-47ed-47cb-baca-358b78d401a0" - }, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "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.*" - ] - }, + "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": [], + "source": [ + "# Your code here\n", + "import pandas as pd\n", + "\n", + "url=\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file1.csv\"\n", + "df=pd.read_csv(url)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "5d8e66b9", + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": null, - "id": "a52c6dfc-cd11-4d01-bda4-f719fa33e9a4", - "metadata": { - "id": "a52c6dfc-cd11-4d01-bda4-f719fa33e9a4" - }, - "outputs": [], - "source": [ - "# Your code here" + "data": { + "text/plain": [ + "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')" ] - }, + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df=df.rename(columns={df.columns[n]:df.columns[n].strip().replace(\" \",\"_\").lower() for n in range(len(df.columns))})\n", + "df=df.rename(columns={\"st\":\"state\"})\n", + "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": 3, + "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c", + "metadata": { + "id": "3f8ee5cb-50ab-48af-8a9f-9a389804033c" + }, + "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": [ + "gender\n", + "F 457\n", + "M 413\n", + "Male 39\n", + "female 28\n", + "Femal 17\n", + "Name: count, dtype: int64" ] - }, + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "df[\"gender\"].value_counts()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "8636992e", + "metadata": {}, + "outputs": [], + "source": [ + "gender_values={\n", + " \"F\":\"F\",\n", + " \"Femal\":\"F\",\n", + " \"female\":\"F\",\n", + " \"Male\":\"M\"}\n", + "\n", + "df[\"gender\"]=df[\"gender\"].replace(gender_values)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "246eaf13", + "metadata": {}, + "outputs": [], + "source": [ + "df[\"state\"].value_counts() #show the values in column\n", + "state_values={ #creates a dictionary with the values and corrections\n", + " \"Oregon\":\"Oregon\",\n", + " \"California\":\"California\",\n", + " \"Cali\":\"California\",\n", + " \"Arizona\":\"Arizona\",\n", + " \"AZ\":\"Arizona\",\n", + " \"Washington\":\"Washington\",\n", + " \"WA\":\"Washington\",\n", + " \"Nevada\":\"Nevada\",\n", + " }\n", + "\n", + "df[\"state\"]=df[\"state\"].replace(state_values) #replace with correct values " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "14a96863", + "metadata": {}, + "outputs": [], + "source": [ + "df[\"education\"].value_counts() #show the values in column\n", + "education_values={ #creates a dictionary with the values and corrections\n", + " \"Bachelors\":\"Bachelor\"\n", + " }\n", + "\n", + "df[\"state\"]=df[\"state\"].replace(state_values) #replace with correct values " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "a1982307", + "metadata": {}, + "outputs": [], + "source": [ + "df[\"customer_lifetime_value\"]=df[\"customer_lifetime_value\"].str.replace(\"%\",\"\")\n", + "#Replace % caracter with none" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "99565b55", + "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": [ + "vehicle_class\n", + "Four-Door Car 576\n", + "Two-Door Car 205\n", + "SUV 199\n", + "Luxury 91\n", + "Name: count, dtype: int64" ] - }, + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[\"vehicle_class\"].value_counts() #show the values in column\n", + "vehicle_class_values={ #creates a dictionary with the values and corrections\n", + " \"Sports Car\":\"Luxury\",\n", + " \"Luxury SUV\":\"Luxury\",\n", + " \"Luxury Car\":\"Luxury\",\n", + " }\n", + "\n", + "df[\"vehicle_class\"]=df[\"vehicle_class\"].replace(vehicle_class_values) #replace with correct values \n", + "df[\"vehicle_class\"]=df[\"vehicle_class\"].astype(\"object\")\n", + "df[\"vehicle_class\"].value_counts()" + ] + }, + { + "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": 19, + "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9", + "metadata": { + "id": "eb8f5991-73e9-405f-bf1c-6b7c589379a9" + }, + "outputs": [], + "source": [ + "# changing customer lifetime calue data type\n", + "df[\"customer_lifetime_value\"]=df[\"customer_lifetime_value\"].astype(\"float64\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "2b47036b", + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": null, - "id": "211e82b5-461a-4d6f-8a23-4deccb84173c", - "metadata": { - "id": "211e82b5-461a-4d6f-8a23-4deccb84173c" - }, - "outputs": [], - "source": [ - "# Your code here" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 4008 entries, 0 to 4007\n", + "Data columns (total 11 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 customer 4008 non-null object \n", + " 1 state 4008 non-null object \n", + " 2 gender 4008 non-null object \n", + " 3 education 4008 non-null object \n", + " 4 customer_lifetime_value 4008 non-null float64\n", + " 5 income 4008 non-null float64\n", + " 6 monthly_premium_auto 4008 non-null float64\n", + " 7 number_of_open_complaints 4008 non-null int32 \n", + " 8 policy_type 4008 non-null object \n", + " 9 vehicle_class 4008 non-null object \n", + " 10 total_claim_amount 4008 non-null float64\n", + "dtypes: float64(4), int32(1), object(6)\n", + "memory usage: 328.9+ KB\n" + ] } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "colab": { - "provenance": [] + ], + "source": [ + "df[\"number_of_open_complaints\"].unique()\n", + "number_of_open_complaints_values={ #creates a dictionary with the values and corrections\n", + " \"1/0/00\":\"0\",\n", + " \"1/2/00\":\"2\",\n", + " \"1/1/00\":\"1\",\n", + " \"1/3/00\":\"3\",\n", + " \"1/5/00\":\"5\",\n", + " \"1/4/00\":\"4\",\n", + " }\n", + "df.fillna(0, inplace=True)\n", + "df[\"number_of_open_complaints\"]=df[\"number_of_open_complaints\"].replace(number_of_open_complaints_values) #replace with correct values \n", + "df[\"number_of_open_complaints\"]=df[\"number_of_open_complaints\"].astype(int)\n", + "df.info()" + ] + }, + { + "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": 97, + "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e", + "metadata": { + "id": "f184fc35-7831-4836-a0a5-e7f99e01b40e" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Index: 1072 entries, 0 to 1071\n", + "Data columns (total 11 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 customer 1072 non-null object \n", + " 1 state 1072 non-null object \n", + " 2 gender 1072 non-null object \n", + " 3 education 1072 non-null object \n", + " 4 customer_lifetime_value 1072 non-null float64\n", + " 5 income 1072 non-null float64\n", + " 6 monthly_premium_auto 1072 non-null float64\n", + " 7 number_of_open_complaints 1072 non-null int32 \n", + " 8 policy_type 1072 non-null object \n", + " 9 vehicle_class 1072 non-null object \n", + " 10 total_claim_amount 1072 non-null float64\n", + "dtypes: float64(4), int32(1), object(6)\n", + "memory usage: 96.3+ KB\n" + ] } + ], + "source": [ + "# Your code here\n", + "\n", + "df=df.drop_duplicates()\n", + "df.info()" + ] + }, + { + "cell_type": "markdown", + "id": "98416351-e999-4156-9834-9b00a311adfa", + "metadata": { + "id": "98416351-e999-4156-9834-9b00a311adfa" + }, + "source": [ + "## Exercise 5: Dealing with duplicates" + ] + }, + { + "cell_type": "markdown", + "id": "ea0816a7-a18e-4d4c-b667-a8452a800bd1", + "metadata": { + "id": "ea0816a7-a18e-4d4c-b667-a8452a800bd1" + }, + "source": [ + "Use the `.duplicated()` method to identify any duplicate rows in the dataframe.\n", + "\n", + "Decide on a strategy for handling the duplicates. Options include:\n", + "- Dropping all duplicate rows\n", + "- Keeping only the first occurrence of each duplicated row\n", + "- Keeping only the last occurrence of each duplicated row\n", + "- Dropping duplicates based on a subset of columns\n", + "- Dropping duplicates based on a specific column\n", + "\n", + "Implement your chosen strategy using the `drop_duplicates()` function.\n", + "\n", + "Verify that your strategy has successfully handled the duplicates by checking for duplicates again using `.duplicated()`.\n", + "\n", + "Remember to document your process and explain your reasoning for choosing a particular strategy for handling duplicates.\n", + "\n", + "Save the cleaned dataset to a new CSV file.\n", + "\n", + "*Hint*: *after dropping duplicates, reset the index to ensure consistency*." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "1929362c-47ed-47cb-baca-358b78d401a0", + "metadata": { + "id": "1929362c-47ed-47cb-baca-358b78d401a0" + }, + "outputs": [], + "source": [ + "# Your code here" + ] + }, + { + "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": 12, + "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": 13, + "id": "211e82b5-461a-4d6f-8a23-4deccb84173c", + "metadata": { + "id": "211e82b5-461a-4d6f-8a23-4deccb84173c" + }, + "outputs": [], + "source": [ + "# Your code here" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" }, - "nbformat": 4, - "nbformat_minor": 5 + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 }