79. Mask Personal Data Before Training
Difficulty: Easy · Topics: Data Prep for AI, Strings, Filtering & Selection
Support tickets in tickets (ticket_id, body) are going into a fine-tuning set for a support assistant. Before that, personal data in body must be replaced:
- Email addresses become
[EMAIL]. - Indian mobile numbers become
[PHONE]: 10 digits starting with 6, 7, 8 or 9, optionally written with+91in front (with or without a space or hyphen after it).
Other numbers must stay as they are: order numbers, amounts and 12-digit tracking numbers are useful to the model. Return ticket_id, body.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
tickets
| ticket_id | body |
|---|---|
| 1 | Please call me on 9876543210 about order 4521. |
| 2 | My email is asha.k@example.com, refund 1200 please |
| 3 | Tracking 129876543210 shows delivered |
Expected output
| ticket_id | body |
|---|---|
| 1 | Please call me on [PHONE] about order 4521. |
| 2 | My email is [EMAIL], refund 1200 please |
| 3 | Tracking 129876543210 shows delivered |
Hints
Hint 1
F.regexp_replace(col, pattern, replacement) replaces every match. Nest two calls, one per kind of data.Hint 2
An email pattern:[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}. Mask emails first: they can contain digits.Hint 3
A 12-digit tracking number contains 10 digits in a row too. Require that the phone number is not touching other digits:(?<![0-9]) before it and (?![0-9]) after it.Learn the concepts
- regexp_extract · 8 min read. Extract, filter, replace and collect with a regular expression, and what you get when nothing matches.
- select · 6 min read. Choose, rename and compute columns, and why select beats a chain of withColumn calls.
PySpark functions you'll practise
- select
- regexp_replace
Related problems
- Remove Near-Duplicate Documents · Medium · Data Prep for AI, Window Functions, Strings
- Filter Low-Quality Training Text · Medium · Data Prep for AI, Strings, Null Handling
- Split Train and Test With No User in Both · Medium · Data Prep for AI, Joins, Aggregations
- Fit Conversations into a Context Window · Hard · Data Prep for AI, Window Functions, Joins
- Build Preference Pairs from Ratings · Hard · Data Prep for AI, Window Functions, Joins
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice