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?

1 Answers1

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))
anon01
  • 10,618
  • 8
  • 35
  • 58
  • 1
    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. – Priyankar Bose Aug 20 '20 at 17:52