Personal loans are a lucrative revenue stream for banks. The typical interest rate of a two-year loan in the United Kingdom is around 10%. This might not sound like a lot, but in September 2022 alone UK consumers borrowed around £1.5 billion, which would mean approximately £300 million in interest generated by banks over two years!
You have been asked to work with a bank to clean the data they collected as part of a recent marketing campaign, which aimed to get customers to take out a personal loan. They plan to conduct more marketing campaigns going forward so would like you to ensure it conforms to the specific structure and data types that they specify so that they can then use the cleaned data you provide to set up a PostgreSQL database, which will store this campaign's data and allow data from future campaigns to be easily imported.
They have supplied you with a csv file called "bank_marketing.csv", which you will need to clean, reformat, and split the data, saving three final csv files. Specifically, the three files should have the names and contents as outlined below:
client.csv
client.csv| column | data type | description | cleaning requirements |
|---|---|---|---|
client_id | integer | Client ID | N/A |
age | integer | Client's age in years | N/A |
job | object | Client's type of job | Change "." to "_" |
marital | object | Client's marital status | N/A |
education | object | Client's level of education | Change "." to "_" and "unknown" to np.NaN |
credit_default | bool | Whether the client's credit is in default | Convert to boolean data type:1 if "yes", otherwise 0 |
mortgage | bool | Whether the client has an existing mortgage (housing loan) | Convert to boolean data type:1 if "yes", otherwise 0 |
campaign.csv
campaign.csv| column | data type | description | cleaning requirements |
|---|---|---|---|
client_id | integer | Client ID | N/A |
number_contacts | integer | Number of contact attempts to the client in the current campaign | N/A |
contact_duration | integer | Last contact duration in seconds | N/A |
previous_campaign_contacts | integer | Number of contact attempts to the client in the previous campaign | N/A |
previous_outcome | bool | Outcome of the previous campaign | Convert to boolean data type:1 if "success", otherwise 0. |
campaign_outcome | bool | Outcome of the current campaign | Convert to boolean data type:1 if "yes", otherwise 0. |
last_contact_date | datetime | Last date the client was contacted | Create from a combination of day, month, and a newly created year column (which should have a value of 2022); Format = "YYYY-MM-DD" |
economics.csv
economics.csv| column | data type | description | cleaning requirements |
|---|---|---|---|
client_id | integer | Client ID | N/A |
cons_price_idx | float | Consumer price index (monthly indicator) | N/A |
euribor_three_months | float | Euro Interbank Offered Rate (euribor) three-month rate (daily indicator) | N/A |
import pandas as pd
import numpy as np
df_raw = pd.read_csv("bank_marketing.csv")df_raw.columnsdf_raw.dtypesdf_raw.month.uniqueETL Processes:
ETL (Extract, Transform, Load) Processes
ETL is a process that involves extracting data from various sources, transforming it into a suitable format, and loading it into a destination system. In this context, we will perform ETL operations on the df_raw and df_economics dataframes to prepare them for analysis.
Step 1: Extract
The extraction step involves loading the data from the source files or databases. In our case, the data has already been loaded into the df_raw and df_economics dataframes.
Step 2: Transform
Transformation involves cleaning and structuring the data. This may include operations such as:
- Handling missing values
- Converting data types
- Merging datasets
- Creating new calculated fields
Let's perform some basic transformations on our data.
# Check for null values -- perfect ! no null values
df_raw.isnull().sum()Client Data:
df_client = df_raw[['client_id', 'age', 'job', 'marital', 'education', 'credit_default', 'mortgage']]
df_client.head()## Transformation process
## Replace . with _ on job
df_client['job'] = df_client.job.str.replace('.', '_')
## education column
## Replace . with _ on education
df_client['education'] = df_client.education.str.replace('.', '_')
## Replace unknown in credit_default with np.NaN
df_client['education'] = df_client.education.replace('unknown', np.NaN)
# Clean and convert client columns to bool data type
for col in ["credit_default", "mortgage"]:
df_client[col] = df_client[col].map({"yes": 1,
"no": 0,
"unknown": 0})
df_client[col] = df_client[col].astype(bool)
df_client.head(20)Campaign Data:
There are 7 columns in this dataset.