What is the meaning of the following code from the Learn Data Analysis with Python book? Can someone elaborate on the first 10-12 lines. Why return s[-9] or in other words assign to ssn? Negative index? As I understand, first replacing hyphens with a space. Then, splitting at the space and rejoin? If number of digits less than 9 and not 'missing' then .. why add the zeros? Then what next?
The ssns are:
The ssns are:
ssns = ['867-53-0909','333-22-4444','123-12-1234',
'777-93-9311','123-12-1423']
def right(s, amount):
return s[-amount]
def standardize_ssn(ssn):
try:
ssn = ssn.replace("-","")
ssn = "".join(ssn.split())
if len(ssn)<9 and ssn != 'Missing':
ssn="000000000" + ssn
ssn=right(ssn,9)
except:
pass
return ssn
df.ssn = df.ssn.apply(standardize_ssn)
df
