Skip to content
Advertisement

Replace decimals in floating point numbers

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

JavaScript

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

You can stack and replace as string:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement