pd.Series.to_list()更改dtype [英] pd.Series.to_list() changing dtype

查看:69
本文介绍了pd.Series.to_list()更改dtype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在colab上编程时,我会不断遇到此问题:

When I am programming on colab, I keep running into this issue:

这是我的df:

0                1
0    [2.7436598593417045e-05, 3.731542193080655e-05]
1    [8.279973504084787e-05, 2.145002145002145e-05]
2    [0.00022534319714215346, 0.0002031172259231674]
3    [3.239841667031943e-05, 2.7771297808289177e-05]
4    [0.00011311134356928321, 9.428422928088026e-05]

我想将数据从df [1]获取到列表列表中,以便可以将其输入模型中.为此,我运行:

I want to get the data from df[1] into a list of lists so I can feed it into my model. To do so, I run:

df[1].to_list()

我得到:

['[2.7436598593417045e-05, 3.731542193080655e-05]',
 '[8.279973504084787e-05, 2.145002145002145e-05]',
 '[0.00022534319714215346, 0.00020311722592316746]',
 '[3.239841667031943e-05, 2.7771297808289177e-05]',
 '[0.00011311134356928321, 9.428422928088026e-05]'] 

这是不能用于输入模型的字符串列表.我一直在本地使用此代码,并且效果很好,但是在colab上我得到了这个结果.有任何想法吗?我想要的结果是:

which is a list of strings which I cannot use to feed into the model. I use this code all the time locally and it works fine, but on colab I get this result. Any ideas? The result I want is:

[[2.7436598593417045e-05, 3.731542193080655e-05],
 [8.279973504084787e-05, 2.145002145002145e-05],
 [0.00022534319714215346, 0.00020311722592316746],
 [3.239841667031943e-05, 2.7771297808289177e-05],
 [0.00011311134356928321, 9.428422928088026e-05]] 

推荐答案

尝试 ast.literal_eval

from ast import literal_eval

df[1].map(literal_eval).to_list()

[[2.7436598593417045e-05, 3.731542193080655e-05],
 [8.279973504084787e-05, 2.145002145002145e-05],
 [0.00022534319714215346, 0.00020311722592316746],
 [3.239841667031943e-05, 2.7771297808289177e-05],
 [0.00011311134356928321, 9.428422928088026e-05]]

这篇关于pd.Series.to_list()更改dtype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆