-1

I am trying to convert this column type into float from object type. How to solve it?

import pandas as pd

df = pd.DataFrame({'col1': ['[-0.8783137, 0.05478287, -0.08827557, 0.69203985, 0.06209986]', 
                            '[0.31444644, -0.6546649, 0.7211526, 0.9819127, 0.74042267]']})

3 Answers3

1

EDIT1: If need convert list of strings to list of floats:

#change sample data
df = pd.DataFrame({'col1': [['-0.8783137', '0.05478287', '-0.08827557', '0.69203985', '0.06209986'], 
                            ['0.31444644', '-0.6546649', '0.7211526', '0.9819127', '0.74042267']]})

#dtype of lists is object
#https://stackoverflow.com/a/42672574/2901002
print (df['col1'].dtype)
object

#first value of column col1
print (df.loc[0, 'col1'])
['-0.8783137', '0.05478287', '-0.08827557', '0.69203985', '0.06209986']

#type of first value of column col1 is list
print (type(df.loc[0, 'col1']))
<class 'list'>

#first value of column col1 and first value of list
print (df.loc[0, 'col1'][0])
-0.8783137

#first value of column col1 and type of first value of list
print (type(df.loc[0, 'col1'][0]))
<class 'str'>

df['col1'] = df['col1'].apply(lambda x: [float(y) for y in x])
#another solution
df['col1'] = [[float(y) for y in x] for x in df['col1']]

print (df)
                                                col1
0  [-0.8783137, 0.05478287, -0.08827557, 0.692039...
1  [0.31444644, -0.6546649, 0.7211526, 0.9819127,...

#dtype of lists is object
#https://stackoverflow.com/a/42672574/2901002
print (df['col1'].dtype)
object
    
#first value of column col1
print (df.loc[0, 'col1'])
[-0.8783137, 0.05478287, -0.08827557, 0.69203985, 0.06209986]

#type of first value of column col1 is list
print (type(df.loc[0, 'col1']))
<class 'list'>

#first value of column col1 and first value of list
print (df.loc[0, 'col1'][0])
-0.8783137

#first value of column col1 and type of first value of list
print (type(df.loc[0, 'col1'][0]))
<class 'float'>

EDIT2: If need DataFrame from lists - each list has same length:

df2 = pd.DataFrame(df['col1'].tolist(), index=df.index).astype(float)
print (df2)
          0         1         2         3         4
0 -0.878314  0.054783 -0.088276  0.692040  0.062100
1  0.314446 -0.654665  0.721153  0.981913  0.740423
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • **Comments have been [moved to chat](https://chat.stackoverflow.com/rooms/253211/discussion-on-answer-by-jezrael-how-to-convert-column-data-type-object-to-float); please do not continue the discussion here.** Before posting a comment below this one, please review the [purposes of comments](/help/privileges/comment). Comments that do not request clarification or suggest improvements usually belong as an [answer](/help/how-to-answer), on [meta], or in [chat]. Comments continuing discussion may be removed. – Samuel Liew Apr 18 '23 at 12:46
0

you can try -

df['col1'] = df['col1'].astype('float')
Yash Mehta
  • 2,025
  • 3
  • 9
  • 20
0
import pandas as pd
import json

df = pd.DataFrame({'col1': ['[-0.8783137, 0.05478287, -0.08827557, 0.69203985, 0.06209986]', 
                            '[0.31444644, -0.6546649, 0.7211526, 0.9819127, 0.74042267]']})

df['col1'] = [json.loads(e) for e in df['col1']]

Check first value

>>> print(type(df.iloc[0,0]))
<class 'list'>

This is a list of float values.

Laurent B.
  • 1,653
  • 1
  • 7
  • 16