Home > Net >  Is there a way of unnesting a column with a list of dictionaries into a pandas Dataframe
Is there a way of unnesting a column with a list of dictionaries into a pandas Dataframe

Time:11-25

This is how my data looks like. I tried everything from turning into a list then into a dataframe but no use.

["[[{'uuid': '3cb5da6c-6db2-4893-9ebb-39443a7c83be', 'answers': 'Vibinators', 'votes': '74'}, {'uuid': '564b3357-df5f-4543-bd07-fa0c3c9401de', 'answers': 'I AM’s', 'votes': '139'}]]"]

CodePudding user response:

solution:

import ast
data = ["[[{'uuid': '3cb5da6c-6db2-4893-9ebb-39443a7c83be', 'answers': 'Vibinators', 'votes': '74'}, {'uuid': '564b3357-df5f-4543-bd07-fa0c3c9401de', 'answers': 'I AM’s', 'votes': '139'}]]"]
df = pd.DataFrame(ast.literal_eval(data[0])[0])

df:

                                   uuid     answers votes
0  3cb5da6c-6db2-4893-9ebb-39443a7c83be  Vibinators    74
1  564b3357-df5f-4543-bd07-fa0c3c9401de      I AM’s   139
  • Related