Skip to Content
Mastering Marketing Data Science
book

Mastering Marketing Data Science

by Iain Brown
April 2024
Beginner to intermediate
432 pages
12h 45m
English
Wiley
Content preview from Mastering Marketing Data Science

EXERCISE 2.1: DATA CLEANING AND TRANSFORMATION

Objective: Clean and transform a dataset to prepare it for analysis.

Tasks:

  1. Handle missing values (NaN) in the “data_cleaning_transfomation.csv” dataset.
  2. Convert the ‘Last Login Date’ from a string to a datetime object.
  3. Create a new feature, ‘Monthly Spend per Day’, by dividing ‘Monthly Spend’ by ‘Subscription Length’.

Steps:

  1. Importing Required Libraries:
    1. import pandas as pd
    • pandas is used for data manipulation and analysis.
  2. Loading the Data:
    2. data_exercise_1 = pd.read_csv('path_to_csv_file')

    This line of code reads the CSV file containing the data into a Pandas DataFrame, enabling us to work with the data in Python.

  3. Handling Missing Values:
    • Filling Missing ‘Age’ Values:
    3. mean_age = data_exercise_1['Age'].mean()
    4. data_exercise_1['Age'].fillna(mean_age, inplace=True)

    Here, we calculate the mean of the ‘Age’ column and fill missing values (NaN) in the ‘Age’ column with this mean. This approach is chosen as age data typically follows a normal distribution, making the mean a good estimate for missing values.

    • Filling Missing ‘Monthly Spend’ Values:
    5. median_monthly_spend = data_exercise_1['Monthly Spend ($)'].median()
    6. data_exercise_1['Monthly Spend ($)'].fillna(median_monthly_spend, inplace=True)
    

    We fill missing values in ‘Monthly Spend ($)’ with the median, because financial data often has outliers, and the median is less sensitive to them compared to the mean.

    • Filling Missing ‘Feedback Score’ Values:
    7. mode_feedback ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Data Science for Marketing Analytics - Second Edition

Data Science for Marketing Analytics - Second Edition

Mirza Rahim Baig, Gururajan Govindan, Vishwesh Ravi Shrimali
Data Science for Business

Data Science for Business

Foster Provost, Tom Fawcett
Data Analytics for Marketing

Data Analytics for Marketing

Guilherme Diaz-Bérrio

Publisher Resources

ISBN: 9781394258710Purchase Link