I have the below data and am obtaining the expected output column for it.
DATA EXPECTED_OUTPUT EXPLANATION
----------------------------------------------------------------------------------------------
123 0000000123 7 leading 0s to get to 10 characters.
nan None If data is not numeric, then null/None output.
123a None If data is not numeric, then null/None output.
1111111111119 1111111119 If data length >10, truncate to right 10 characters.
0 0000000000 9 leading 0s to get to 10 characters.
123.0 0000000123 Remove decimal part and leading 0s to get to 10 characters.
I have 3 ways currently of doing so, but I'm unsure whether they are the optimal solution to handle all data possibilities.
# Option 1
df['DATA'] =pd.to_numeric(df['DATA'], errors='coerce').fillna(0).\
astype(np.int64).apply(str).str.zfill(10)
# Option 2
df['DATA'] = df['DATA'].map('{:0>10}'.format).astype(str).\
str.slice(0, 10)
# Option 3
df['DATA'] = df['DATA'].astype(str).str.split('.', expand=True)[0].str\
.zfill(10).apply(lambda x_trunc: x_trunc[:10])
Any help on how to improve the way of doing this will be truly appreciated.