Skip to content

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

columndata typedescriptioncleaning requirements
client_idintegerClient IDN/A
ageintegerClient's age in yearsN/A
jobobjectClient's type of jobChange "." to "_"
maritalobjectClient's marital statusN/A
educationobjectClient's level of educationChange "." to "_" and "unknown" to np.NaN
credit_defaultboolWhether the client's credit is in defaultConvert to boolean data type:
1 if "yes", otherwise 0
mortgageboolWhether the client has an existing mortgage (housing loan)Convert to boolean data type:
1 if "yes", otherwise 0

campaign.csv

columndata typedescriptioncleaning requirements
client_idintegerClient IDN/A
number_contactsintegerNumber of contact attempts to the client in the current campaignN/A
contact_durationintegerLast contact duration in secondsN/A
previous_campaign_contactsintegerNumber of contact attempts to the client in the previous campaignN/A
previous_outcomeboolOutcome of the previous campaignConvert to boolean data type:
1 if "success", otherwise 0.
campaign_outcomeboolOutcome of the current campaignConvert to boolean data type:
1 if "yes", otherwise 0.
last_contact_datedatetimeLast date the client was contactedCreate 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

columndata typedescriptioncleaning requirements
client_idintegerClient IDN/A
cons_price_idxfloatConsumer price index (monthly indicator)N/A
euribor_three_monthsfloatEuro Interbank Offered Rate (euribor) three-month rate (daily indicator)N/A
import pandas as pd
import numpy as np
df = pd.read_csv("bank_marketing.csv")

for col in ["credit_default", "mortgage", "previous_outcome", "campaign_outcome"]:
    print(col)
    print("--------------")
    print(df[col].value_counts())
# Read the original CSV file
df = pd.read_csv("bank_marketing.csv")

# Select relevant columns and create a new DataFra
df1 = df[["client_id","age","job","marital","education","credit_default","mortgage"]]

# Replace periods in 'job' and 'education' columns with hyphens
df1["job"] = df1["job"].str.replace(".","-")
df1["education"] = df1["education"].str.replace(".","-")

# Replace 'unknown' in 'education' column with NaN
df1["education"] = df1["education"].replace("unknown",np.nan)

# Clean the 'credit_default' & 'mortgage' column, convert 'yes' to 1, and 'no' and 'unknown' to 0 then convert to boolean data type
df1["credit_default"] = df1["credit_default"].map({"yes":1, "no": 0, "unknown": 0}).astype(bool)
df1["mortgage"] = df1["mortgage"].map({"yes":1, "no": 0, "unknown": 0}).astype(bool)

# Save the cleaned data to a new CSV file
df1.to_csv("client.csv", index=False)
# import pandas as pd

# Read the original CSV file
# df = pd.read_csv("bank_marketing.csv")

# Clean the 'previous_outcome' & 'campaign_outcome' column, convert 'yes' to 1, and 'other values' to 0 then convert to boolean data type
df["previous_outcome"] = df["previous_outcome"].map({"success":1, "failure": 0, "nonexistent": 0}).astype(bool)
df["campaign_outcome"] = df["campaign_outcome"].map({"yes":1, "no": 0}).astype(bool)

# Create year column
df["year"] = '2022'

# Convert the day and month column from the original DataFrame into a string
df["month"] = df["month"].astype(str)
df["day"] = df["day"].astype(str)

# Add the 'year', 'month' & 'day' columns
df["last_contact_date"] = df["year"] + "-" + df["month"] + "-" + df["day"]

# Convert the 'last_contact_date' column to Datetime
df["last_contact_date"] = pd.to_datetime(df['last_contact_date'], format="%Y-%b-%d")

# Select relevant columns and create a DataFrame
df2 = df[["client_id","number_contacts","contact_duration","previous_campaign_contacts","previous_outcome","campaign_outcome","last_contact_date"]]
                                         
# Save the cleaned data to a new CSV file
df2.to_csv("campaign.csv", index=False)
# import pandas as pd

# Read the original CSV file
# df = pd.read_csv("bank_marketing.csv")

# Select the relevant column and create a Dataframe
df3=df[["client_id","cons_price_idx","euribor_three_months"]]

# Save the cleaned data to a new CSV file
df3.to_csv("economics.csv", index=False)