-2

I have a column that was extracted using Pandas. The following column may contain one dictionary or more than one dictionary.

Column B
[{'url': 'mailto:Kim_Do@dmx.com', 'type': 0, 'id': 1021857, 'name': 'KIM Do', 'entryListId': -1}, {'url': 'mailto:Angel_Kong@dmx.com', 'type': 0, 'id': 1023306, 'name': 'Angel Kong', 'entryListId': -1}, {'url': 'mailto:Alex_Do@dmx.com', 'type': 0, 'id': 1023289, 'name': 'Alex Do', 'entryListId': -1}
[{'url': 'mailto:Ray_Chan@dmx.com', 'type': 0, 'id': 1021857, 'name': 'Ray Chan', 'entryListId': -1}, {'url': 'mailto:Paul_Jones@dmx.com', 'type': 0, 'id': 1023306, 'name': 'Paul Jones, 'entryListId': -1}]
nan
nan
[{'url': 'mailto:Ray_Chaudhry@dmx.com', 'type': 0, 'id': 1021857, 'name': 'Ray Chaudhry', 'entryListId': -1}]

What I want back is just the names from the dictionary. So, the output should be as follows:

Column B
Kim Do, Angel Kong, Alex Do, Fred Tome
Ray Chan, Paul Jones
nan
nan
Ray Chaudhry

How can I achieve this. Thank you!

2 Answers2

0

You can use:

df['New'] = df['Column B'].explode().str['name'].dropna().groupby(level=0).agg(', '.join)

Output (New column only):

0    KIM Do, Angel Kong, Alex Do
1           Ray Chan, Paul Jones
2                            NaN
3                            NaN
4                   Ray Chaudhry
mozway
  • 194,879
  • 13
  • 39
  • 75
0

Check out the screenshot

Use this function:

def extract_names(list_data):
    row_names = []
    for n in range(len(list_data)):
        row_names.append(list_data[n]['name'])
    return row_names

store_names = []
col = 0 #column name
for idx, row in df.iterrows():
#     print(idx)
#     print(row[col])
    store_names.append(extract_names(row[col]))
#     print('--')

Now you can store the list as parameter of your choice:

df['Names'] = store_names