0

I am trying to convert string to float in a dataset. The dataset consists of several columns where one of the columns contains entries:

col1 col2
x [1.1, 1.2, 1.3 ...

When I use pd.to_numeric I get the following error: Unable to parse string "[1.1, 1.2........]" at position 0.

**This is a simplified version of my code, where the numbers resemble this 2.1874960973678963

  • What output do you expect? A list of floats? – mozway Aug 01 '21 at 03:50
  • Something like [Pandas DataFrame stored list as string: How to convert back to list](https://stackoverflow.com/q/23111990/15497888) or [How can I save DataFrame as list and not as string](https://stackoverflow.com/q/64183163/15497888) ? – Henry Ecker Aug 01 '21 at 03:56
  • @mozway Yes, I am using a program that reads the csv file and it says that it couldn't convert string to float. – user16470918 Aug 01 '21 at 15:03
  • @HenryEcker I tried those solutions, but its no use. – user16470918 Aug 01 '21 at 15:04
  • @user16470918 I provided an alternative in case you have lists of strings (I briefly saw your comment, then it disappeared, so I don't know if it's still valid) – mozway Aug 01 '21 at 16:02

1 Answers1

1

elements are strings

Assuming you want to convert a string representation of a list of floats ("[1.1, 1.2, 1.3]") to a list of floats:

input:

df = pd.DataFrame({'col1':['x'], 'col2':'[1.1, 1.2, 1.3]'})

processing:

df['col2'] = df['col2'].apply(lambda s: list(map(float, s.strip('[]').split(', '))))

edit: check the link provided by @Henry Ecker, there is a nice solution with ast.literal_eval

import ast
df['col2'] = df['col2'].apply(ast.literal_eval)

elements are lists of strings

If your elements are already lists of strings (['1.1', '1.2', '1.3']), use:

df['col2'] = df['col2'].apply(lambda x: [float(i) for i in x])

or:

df['col2'] = df['col2'].apply(lambda x: list(map(float, x)))
mozway
  • 194,879
  • 13
  • 39
  • 75