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
  • 7,825
  • 16
  • 57
  • 118

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
  • 822,522
  • 95
  • 1,334
  • 1,252