RodrikTheReader
RodrikTheReader

Reputation: 827

How to mask a column in a Pandas Dataframe while displaying it?

I have a Dataframe:

    ID    Name         Salary($)
     0    Alex Jones     44,000
     1    Bob Smith      65,000
     2    Peter Clarke   50,000

In order to protect the privacy of the individuals in this dataset, I want to mask the output of this Dataframe in a Jupyter notebook like this:

    ID    Name         Salary($)
     0    AXXX XXXX     44,000
     1    BXX XXXXX     65,000
     2    PXXXX XXXXX   50,000

Individually replacing characters in each name seems very crude to me. There must be a better approach?

Upvotes: 2

Views: 630

Answers (1)

EdChum
EdChum

Reputation: 394091

You can concatenate the first character by the result of replace all characters in the remaining slice of each name using str.replace:

In[16]:
df['Name'] = df['Name'].str[0] + df['Name'].str[1:].str.replace('\w','X')
df

Out[16]: 
   ID          Name Salary($)
0   0    AXXX XXXXX    44,000
1   1     BXX XXXXX    65,000
2   2  PXXXX XXXXXX    50,000

Upvotes: 3

Related Questions