'How to convert a array of string to array of float using
I have a array as shown below, in that I am having a variable called f, I need to assign the some value for this variable f say 2 and convert this into a floating point array.
(['0.', '0.', '-6.190649155150273*^15 + 0.7747634892904517*f^2',
'2.2098855503598858*^10 + 4.250697125128597*^-7*f^2', '0.', '0.',
'0.', 0.0, 0.0, -1427.5184297531378], dtype=object)
I tried with df.convert_objects(convert_numeric=True) but it is not working
Solution 1:[1]
So I presume that the math expressions for
-6.190649155150273*^15 + 0.7747634892904517*f^2 is
2.2098855503598858*^10 + 4.250697125128597*^-7*f^2 is
And, maybe my presume is wrong, but I understand that you wanna make a math expression written in the string that could run as code.
So, I will recommend SymPy, here is the official website, you can check the detail, I'll give you a little example.
Explain
eval
Python eval() to dynamically evaluate expressions from a string-based or compiled-code-based input.
subs
- dict args as function input, for your example,
{f:2} - be careful, here
fis not a string, but thesymbolsvariable
And also, if I misunderstand the math expressing meaning, you need to replace or regexp by yourself.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | FavorMylikes |

