将熊猫列值更改为另一种格式

时间:2019-06-21 06:31:59

标签: python pandas

我的get数据框中的labels列如下:

test['labels']

对于列(0 ['Edit Distance'] 1 ['Island Perimeter'] 2 ['Longest Substring with At Most K Distinct Ch... 3 ['Valid Parentheses'] 4 ['Intersection of Two Arrays II'] 5 ['N-Queens'] )的字符串表示形式的列中的每个值,我想应用下面的函数将其转换为实际列表。

"['Edit Distance']"

执行此操作的直接方法是什么?

1 个答案:

答案 0 :(得分:4)

使用:

import ast
test['labels'] = test['labels'].apply(ast.literal_eval)
print (test)
                                           labels
0                                 [Edit Distance]
1                              [Island Perimeter]
2  [Longest Substring with At Most K Distinct Ch]
3                             [Valid Parentheses]
4                 [Intersection of Two Arrays II]
5                                      [N-Queens]