I have a column called SSN in a CSV file with values like this
289-31-9165
I need to loop through the values in this column and replace the first five characters so it looks like this
***-**-9165
Here's the code I have so far:
emp_file = "Resources/employee_data1.csv"
emp_pd = pd.read_csv(emp_file)
new_ssn = emp_pd["SSN"].str.replace([:5], "*")
emp_pd["SSN"] = new_ssn
How do I loop through the value and replace just the first five numbers (only) with asterisks and keep the hiphens as is?
Put your asterisks in front, then grab the last 4 digits.
You can simply achieve this with replace() method:
Example dataframe :
borrows from @AkshayNevrekar..
Result:
OR
OR:
Similar to Mr. Me, this will instead remove everything before the first 6 characters and replace them with your new format.
You can use
regex
Output: