Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab02 done #327

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,371 @@
{
"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.*"
]
},
{
"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": {
"tags": [],
"id": "80f846bb-3f5e-4ca2-96c0-900728daca5a"
},
"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": {
"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": []
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading