Siddharth C
Siddharth C

Reputation: 39

How to mask specific values in particular column in Python?

I have a .csv file with 5 columns and about 5000 rows. In a particular column called 'summary' in the .csv file there is credit card numbers along with a few text. It looks like this

hey this job needs to be done asap and pay with card# visa 5611000043310001

I want to read this column, take out the number (maybe by using regular expression) and then mask the last 4 digits and write out the entire row as it is with the masked number like this in a .csv file.

hey this job needs to be done asap and pay with card# visa 561100004331****

How can I do it?

Upvotes: 0

Views: 1343

Answers (2)

Colin Pearse
Colin Pearse

Reputation: 36

The replace function with regex below looks for numbers of exactly 16 digits and masks the last 4 digits.

So this code:

eg_summaries = [
    'blah blah card# visa 5611000043310001',
    'blah blah card# visa 5611000043310001 with text after',
    '5611000043310001',
    'visa: 5611000043310001 and random number > 16 digits: 0011237324763246723487243',
               ]
df = pd.DataFrame({'summary': eg_summaries })
df['summary'].replace(r'\b(\d{12})\d{4}\b', r'\1****', inplace=True, regex=True)
print (df.summary)

should print out this: 0 blah blah card# visa 561100004331**** 1 blah blah card# visa 561100004331**** with text after 2 561100004331**** 3 visa: 561100004331**** and random number > 16 digits: 0011237324763246723487243

Upvotes: 0

Nakor
Nakor

Reputation: 1514

With regex, you could do:

import re

>> s = "hey this job needs to be done asap and pay with card# visa 5611000043310001"
>> re.sub(r"(\d{12})\d{4}",r"\1****",s)

'hey this job needs to be done asap and pay with card# visa 561100004331****'

So basically, (\d{12}) matches the first 12 digits (the parentheses are there to not replace these first 12). And then 4 digits, that we replace by stars. \1 is a placeholder for the first group that is omitted by the replacement, so here it refers to the first 12 digits.

Upvotes: 1

Related Questions