pandas 如何将列数据类型对象转换为浮点型

q3qa4bjr  于 2023-04-18  发布在  其他
关注(0)|答案(3)|浏览(168)

我想把这个列类型从对象类型转换成浮点型,怎么解决?

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]']})
xmd2e60i

xmd2e60i1#

锁定21小时,该答案已被禁止评论,但仍接受其他互动Learn more

EDIT 1:如果需要将字符串列表转换为浮点数列表:

#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'>

EDIT 2:如果需要列表中的DataFrame-每个列表具有相同的长度:

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
2guxujil

2guxujil2#

你可以试试

df['col1'] = df['col1'].astype('float')
v1uwarro

v1uwarro3#

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']]

检查第一个值

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

这是一个浮点值列表。

相关问题