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 |
mortgage | bool | Whether the client has an existing mortgage (housing loan) | Convert to boolean data type |
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 |
campaign_outcome | bool | Outcome of the current campaign | Convert to boolean data type |
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
# Start coding here...
# loading the banking_marketing dataset into pandas using pd.read_csv() as a variable called bank
bank = pd.read_csv("bank_marketing.csv")
# viewing first five rows of economics
print(bank.head())
# checking the shape of data: number of rows and columns
print(bank.shape)
# checking bank columns data type
print(bank.info())
###### creating economics.csv from bank
# creating a dataframe called economics from bank
economics = bank[["client_id", "cons_price_idx", "euribor_three_months"]]
# checking economics columns data type
print(economics.info())
# viewing first five rows of economics
print(economics.head())
# storing as a csv files
economics.to_csv('economics.csv', index=False)
###### creating client.csv from bank
# creating a dataframe called client from bank
client = bank[["client_id", "age", "job", "marital", "education", "credit_default", "mortgage"]]
# replacing "." with "_" in job & education column
client["job"] = client["job"].str.replace(".", "_")
client["education"] = client["education"].str.replace(".", "_")
# replacing "unknown" with np.NaN in education, mortgage & credit_default column
client["education"] = client["education"].replace("unknown", np.NaN)
client["mortgage"] = client["mortgage"].replace("unknown", np.NaN)
client["credit_default"] = client["credit_default"].replace("unknown", np.NaN)
# creating a mapping dictionary
mapping_bool = {"yes": True, "no": False}
# mapping the created dictionary to mortgage & credit_default column using the map()
client["mortgage"] = client["mortgage"].map(mapping_bool)
client["credit_default"] = client["credit_default"].map(mapping_bool)
# changing mortgage & credit_default column data type from object to boolean
client["credit_default"] = client["credit_default"].astype(bool)
client["mortgage"] = client["mortgage"].astype(bool)
# checking client columns data type
print(client.info())
# viewing first five rows of client
print(client.head())
# storing as a csv files
client.to_csv("client.csv", index= False)
###### creating campaign.csv from bank
# creating a dataframe called campaign from bank
campaign = bank[["client_id", "number_contacts", "contact_duration", "previous_campaign_contacts", "previous_outcome", "campaign_outcome", "day", "month"]]
# creating a year column and assigning it to be 2022
campaign["year"] = 2022
# parsing year, month, day as datetime
campaign["last_contact_date"] = pd.to_datetime(campaign["year"].astype(str) + "-" + campaign["month"] + "-" + campaign["day"].astype(str))
# dropping day, month, year columns
campaign = campaign.drop(columns=["day", "month", "year"])
# creating a mapping dictionary
mapp_bool = {"success": True, "failure": False, "nonexistent": False, "no": False, "yes": True}
# mapping the created dictionary to previous_outcome & campaign_outcome column using the map()
campaign["previous_outcome"] = campaign["previous_outcome"].map(mapp_bool)
campaign["campaign_outcome"] = campaign["campaign_outcome"].map(mapp_bool)
# changing previous_outcome & campaign_outcome column data type from object to boolean
campaign["previous_outcome"] = campaign["previous_outcome"].astype(bool)
campaign["campaign_outcome"] = campaign["campaign_outcome"].astype(bool)
# checking client columns data type
print(campaign.info())
# viewing first five rows of campaign
print(campaign.head())
# storing as a csv files
campaign.to_csv("campaign.csv", index=False)