0

I have a df where one of my columns carries a string representation of a np array. Values have variable amounts of whitespace in between them and some have scientific notation and some don't. An example looks like this:

'[1.754785e-03 1.017525e-04 6.074961e-04 1.533449e-03 3.90     9.817408e-01 8.334255e-03 1.835601e-03 6.365406e-04]'

I want the following result:

[1.754785, 1.017525, 6.074961, 1.533449, 3.9, 9.817408e, 8.334255, 1.835601, 6.365406]

You will notice that the scientific notation has been replaced with 6 digit precision, all variable whitespace is used to split values regardless of how many spaces separate, and the values are now comma separated and stored in a list or floats.

1 Answer 1

1

You can try:

df['col']=df['col'].str.strip('[]').str.split().map(lambda x:list(map(float,x)))

OR

from ast import literal_eval

df['col']=df['col'].replace(r"\s+",",",regex=True).map(literal_eval)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.