Skip to content
Advertisement

Replace decimals in floating point numbers

Someone on this platform has already helped me with generating the following code:

col,row = (100,1000) 
a = np.random.uniform(0,10,size=col*row).round(6).reshape(row,col)
mask = (a*1e6+1).astype(int)%10<2
a[mask] += 2e-6

This code ensures that the last decimal is not a 0 or a 9. However, I would like to have no 0’s nor 9’s in all the decimals that are generated (such that it will not be possible to have a number 1.963749 or 3.459007).

It would be fine for all 0’s and 9’s to be, for example, replaced by a 2 (1.263742 and 3.452227 considering the example above). I know that the function replace (0, 2) does not work for the decimal numbers. Is there a way to replace these numbers or should the code be rewritten to make this work?

Advertisement

Answer

Solution using strings (I find it non-elegant but it works).

Assuming this input as pandas DataFrame df:

       col1      col2
0  1.234567  9.999909
1  1.999999  0.120949

You can stack and replace as string:

def rand(x):
    import random
    return random.choice(list('12345678'))

df2 = (df
 .stack()
 .astype(str)
 .str.replace(r'[09](?!.*.)', rand)
 .astype(float)
 .unstack()
 )

Output:

       col1      col2
0  1.234567  9.665236
1  1.184731  0.128345
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement