Home > Net >  How to access values from list of dictionaries in a dataframe?
How to access values from list of dictionaries in a dataframe?

Time:10-13

I have a dataframe that has a column with a list of dictionaries and for each dictionary I want to be able to extract the values and put them in another column as list. Please see the picture below for example which shows only 1 row of the dataframe. so for each title shown on the picture I want to extract the values and put them in a list for all the rows in a dataframe

example

CodePudding user response:

Use ast.literal_eval to convert the string as a list of dict then extract the `title keys from each records:

import ast

df['activities'].apply(lambda x: [d['title'] for d in ast.literal_eval(x)])
  • Related