简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM