将熊猫系列类型从str更改为list / numeric

时间:2020-06-29 11:39:03

标签: python pandas

我有以下数据框:

df_small enter image description here

每个列的类型均为pandas.Series,列中的每个值的类型均为str

df_small['center'][0] # '[(181, 186)]'

我想从每个值中删除字符串引号以访问列表值。

1 个答案:

答案 0 :(得分:1)

根据每个注释的建议,您可以使用ast模块中的literal_eval,可以在此处找到一些文档。在这种情况下,如果并非所有值都为map(lambda x:)(如标题中所建议的那样),我将使用list

import ast
df['center'] = df['center'].map(lambda x: ast.literal_eval(x))