Skip to content

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:

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_idbody
1Please call me on 9876543210 about order 4521.
2My email is asha.k@example.com, refund 1200 please
3Tracking 129876543210 shows delivered

Expected output

ticket_idbody
1Please call me on [PHONE] about order 4521.
2My email is [EMAIL], refund 1200 please
3Tracking 129876543210 shows delivered

Hints

Hint 1F.regexp_replace(col, pattern, replacement) replaces every match. Nest two calls, one per kind of data.
Hint 2An email pattern: [A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}. Mask emails first: they can contain digits.
Hint 3A 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

PySpark functions you'll practise

Related problems

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