1

I have a dataframe where a column is an array of floats. When I am reading the csv file as a pandas dataframe, the particular column is recognized as a string as follows:

'[4816.0, 20422.0, 2015.0, 2020.0, 2025.0, 5799.0, 2000.0, 1996.0, 3949.0, 3488.0]', 
'[13047.0, 7388.0, 16437.0, 2096.0, 13618.0, 2000.0, 1996.0, 23828.0, 6466.0, 1996.0]',....

I want to convert this long character string into an array of floats like this:

[4816.0, 20422.0, 2015.0, 2020.0, 2025.0, 5799.0, 2000.0, 1996.0, 3949.0, 3488.0],
[13047.0, 7388.0, 16437.0, 2096.0, 13618.0, 2000.0, 1996.0, 23828.0, 6466.0, 1996.0],...

Is there a way to do that?

2
  • 3
    Does this answer your question? Change data type of columns in Pandas Commented Aug 19, 2020 at 2:18
  • astype is not general purpose deserialization of strings... Commented Aug 19, 2020 at 2:46

1 Answer 1

1

If possible, it would be best to read the csv correctly (as a list of floats) rather than casting them from the strings. You can however use eval or ast.literal_eval to cast this to a list of floats:

from ast import literal_eval    
df["a"] = df["a"].apply(lambda x: literal_eval(x))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I am curious to know how to read the particular of the csv file as a list of floats while reading the csv file with multiple columns of different data types.

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.