I have a pandas dataframe where three columns are floats (floats64):
Num1 Num2 Num3
0 2345656 3.0 12345.0
1 3456 3.0 3100.0
2 541304 4.0 5432.0
3 NaN NaN NaN
4 12313201308 1.0 99999.0
I want to add leading zeroes to Num2 and Num3 columns to make them look like this:
Num1 Num2 Num3
0 2345656 003 12345
1 3456 003 03100
2 541304 004 05432
3 NaN NaN NaN
4 12313201308 001 99999
I want Num2 to have 3 digits in total including the leading zeroes and Num3 to have 5 digits in total including the leading zeroes, leaving NaNs as they are (or not impacting NaNs). The end goal is to concatenate Num1, Num2, and Num3 to create a new column.
I tried both
df['Num2'].apply(lambda x: '{:05}'.format(x) if pd.notnull(x) else x)
and
df['Num2'].apply(lambda x: x.zfill(5) if pd.notnull(x) else x)
, but they did not add zeroes as I expected. I would appreciate if someone more knowledgable than I am can help me out!