如何将向量的Pandas系列的类型从str更改为数值?

qmb5sa22  于 2023-06-28  发布在  其他
关注(0)|答案(1)|浏览(86)

我确实有一个由固定大小的向量组成的Series,但作为str。我怎样才能改变这个系列的类型为一个数字向量?
以下是本系列的预览:

p.s. a similar question中提供的答案没有帮助。

wz3gfoph

wz3gfoph1#

因为你有nan值,你可以使用pd.eval

out = gen_vec.apply(pd.eval, local_dict={'nan': np.nan})

使用ast模块中的literal_eval

import ast:

out = gen_vec.apply(ast.literal_eval)

输出:

>>> out
0    [[0.6304918890918207, -0.5886238157645294, -0....
1    [[-0.6302182776914216, 0.9368165801475401, 0.7...
2    [[0.6153572001094536, -0.07547153598238743, -0...
3    [[0.1583211249108949, -0.07501481771633367, -0...
4    [[0.9793698091130785, 0.6140448218764745, -0.9...
dtype: object

>>> out.loc[0]
[[0.6304918890918207, -0.5886238157645294, -0.3194771085022785],
 [-0.7222439829639373, 0.682891259912199, -0.9084527274979692],
 [0.9372246370318329, -0.8042811128682565, -0.39435908071826065]]

>>> type(out.loc[0])
list

输入示例:

data = ['[[0.6304918890918207, -0.5886238157645294, -0.3194771085022785], [-0.7222439829639373, 0.682891259912199, -0.9084527274979692], [0.9372246370318329, -0.8042811128682565, -0.39435908071826065]]',
        '[[-0.6302182776914216, 0.9368165801475401, 0.7293141762489015], [-0.10363402231002539, 0.22356716941880794, 0.6796536411142267], [0.739412959837795, 0.3434906849876964, 0.6840523183724572]]',
        '[[0.6153572001094536, -0.07547153598238743, -0.3147739134079086], [-0.4517142976978141, -0.7661353319665889, -0.08218569081022897], [0.21828238409073308, -0.8458822924041092, -0.8100486062713181]]',
        '[[0.1583211249108949, -0.07501481771633367, -0.8430782622316249], [0.11189737816973255, -0.890710343331605, 0.2881597201674384], [-0.8188156405874802, -0.16829948165814113, -0.9222470203602522]]',
        '[[0.9793698091130785, 0.6140448218764745, -0.9485282042022696], [0.7188762127494397, 0.042247790689530884, -0.5645509356734524], [-0.26842956038325627, -0.993030492245303, -0.8585439320376391]]']

gen_vec = pd.Series(data)

相关问题