Someone on this platform has already helped me with generating the following code:
JavaScript
x
5
1
col,row = (100,1000)
2
a = np.random.uniform(0,10,size=col*row).round(6).reshape(row,col)
3
mask = (a*1e6+1).astype(int)%10<2
4
a[mask] += 2e-6
5
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
:
JavaScript
1
4
1
col1 col2
2
0 1.234567 9.999909
3
1 1.999999 0.120949
4
You can stack and replace as string:
JavaScript
1
12
12
1
def rand(x):
2
import random
3
return random.choice(list('12345678'))
4
5
df2 = (df
6
.stack()
7
.astype(str)
8
.str.replace(r'[09](?!.*.)', rand)
9
.astype(float)
10
.unstack()
11
)
12
Output:
JavaScript
1
4
1
col1 col2
2
0 1.234567 9.665236
3
1 1.184731 0.128345
4