1

The labels column in my test['labels'] dataframe, looks like:

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']

For each value in the column, which is a string representation of list ("['Edit Distance']"), I want to apply the function below to convert it into an actual list.

ast.literal_eval(VALUE HERE)

What is a straightforward way to do this?

Dawn17
  • 6,673
  • 11
  • 45
  • 105

1 Answers1

4

Use:

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]
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090