when importing in pandas the data looks like that:
>>> BOM.PriceQty['substrate']
'[200.0, 300.0, 500.0]'
how do I convert it to list of floats? tried convert_objact:
>>> BOM.PriceQty['substrate'].convert_object(convert_numeric=True)
Traceback (most recent call last): File "", line 1, in AttributeError: 'str' object has no attribute 'convert_object'
Thanks!
This would nicely convert a string representing a list of floats to an actual list of floats:
s = '[200.0, 300.0, 500.0]'
l = [float(x.strip(' []')) for x in s.split(',')]
The strip
function removes any ' '
, '['
, and ']'
characters resulting from the split.
Result:
In [1]: l
Out[1]: [200.0, 300.0, 500.0]
If you want to apply this transformation to the entire column, apply
it as a lambda
function:
BOM.PriceQty.apply(lambda s: [float(x.strip(' []')) for x in s.split(',')])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With