1

I have a Dataframe in the below format:

id, value
101, [{'id': 'ZWJ', 'type': 'user_reference', 'summary': 'Person 1'}]
102, [{'id': 'ZWS', 'type': 'user_reference', 'summary': 'Person 2'}]

I am trying to extract the value tagged to summary in each row.

Expected output :

id, name
101, Person 1
102, Person 2
jpp
  • 159,742
  • 34
  • 281
  • 339
scott martin
  • 1,253
  • 1
  • 14
  • 36
  • Possible duplicate of [Expand nested list of dictionaries in a pandas dataframe column](https://stackoverflow.com/questions/55093729/expand-nested-list-of-dictionaries-in-a-pandas-dataframe-column) or [Convert list of dictionaries to a pandas DataFrame](https://stackoverflow.com/questions/20638006/convert-list-of-dictionaries-to-a-pandas-dataframe) – jpp Apr 06 '20 at 07:51

2 Answers2

2

Use str[0] for get first list and then Series.str.get for value summary:

df['name'] = df['value'].str[0].str.get('summary')

print (df)
    id                                              value      name
0  101  [{'id': 'ZWJ', 'type': 'user_reference', 'summ...  Person 1
1  102  [{'id': 'ZWS', 'type': 'user_reference', 'summ...  Person 2

Details:

print (df['value'].str[0])
0    {'id': 'ZWJ', 'type': 'user_reference', 'summa...
1    {'id': 'ZWS', 'type': 'user_reference', 'summa...
Name: value, dtype: object
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Got this fixed by

df['name'] = df.value(operator.itemgetter(0)).apply(operator.itemgetter('summary'))
scott martin
  • 1,253
  • 1
  • 14
  • 36