将数据框中的字符串值拆分为numpy浮点值

时间:2018-12-26 08:53:24

标签: python pandas numpy

我有一个数据框,它的列具有浮点值,但采用字符串格式。 如何将字符串值转换为float并将其存储在numpy数组中?

例如:

0   252485  '11.928911999999999 4.9965290000000016 0.0 0.0 ...'     '2.490541199999999 -6.533438 3.7505536 4.933191...' 1 0

这是数据框的第一行 我希望它看起来像

[[11.928911999999999 4.9965290000000016 0.0 0.0 ... 2.490541199999999 -6.533438 3.7505536 4.933191...]]

enter image description here

1 个答案:

答案 0 :(得分:0)

让我知道这是否对您有用:

import pandas as pd
import numpy as np
df = pd.DataFrame([{'0': '5421736', '1': '12.9839834 1.29748374 4.8293'},
                   {'0': '13423',  '1': '19.43434 98.8934783674545 5.3456789'},
                   {'0': '39423',  '1': '9.423283434 0.563763648 123.17637364 34.8973493740'}])

df['new_1'] = df['1'].map(lambda x: [float(i) for i in x.split()])

#test the output:
df.iloc[0]['new_1']