How can use ast.literal_eval on my dataframe? I keep getting error: ‘malformed node or string: ‘ + repr(node)

I have been trying to make dictionaries out of one of thecolumns of my dataframe.

1     [{'id': 12, 'name': 'Adventure'}, {'id': 14, '...
2     [{'id': 10749, 'name': 'Romance'}, {'id': 35, ...
3     [{'id': 35, 'name': 'Comedy'}, {'id': 18, 'nam...
4                        [{'id': 35, 'name': 'Comedy'}]
5     [{'id': 28, 'name': 'Action'}, {'id': 80, 'nam...
6     [{'id': 35, 'name': 'Comedy'}, {'id': 10749, '...
7     [{'id': 28, 'name': 'Action'}, {'id': 12, 'nam...
8     [{'id': 28, 'name': 'Action'}, {'id': 12, 'nam...
9     [{'id': 12, 'name': 'Adventure'}, {'id': 28, '...
10    [{'id': 35, 'name': 'Comedy'}, {'id': 18, 'nam...
11    [{'id': 35, 'name': 'Comedy'}, {'id': 27, 'nam...
12    [{'id': 10751, 'name': 'Family'}, {'id': 16, '...
13    [{'id': 36, 'name': 'History'}, {'id': 18, 'na...
14    [{'id': 28, 'name': 'Action'}, {'id': 12, 'nam...
15    [{'id': 18, 'name': 'Drama'}, {'id': 80, 'name...
16    [{'id': 18, 'name': 'Drama'}, {'id': 10749, 'n...
17    [{'id': 80, 'name': 'Crime'}, {'id': 35, 'name...
18    [{'id': 80, 'name': 'Crime'}, {'id': 35, 'name...
19    [{'id': 28, 'name': 'Action'}, {'id': 35, 'nam...
Name: genres, dtype: string

I know I can not use ast.literal_eval because some rows have1 item, others have 2 and others 3. And I don't know how to "re-form" the column. I have tried filling the rows.

I will appreciate your help.

Thank you!



Read more here: https://stackoverflow.com/questions/64513678/how-can-use-ast-literal-eval-on-my-dataframe-i-keep-getting-error-malformed-n

Content Attribution

This content was originally published by Miniphant at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: