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:[email protected]', 'type': 0, 'id': 1021857, 'name': 'KIM Do', 'entryListId': -1}, {'url': 'mailto:[email protected]', 'type': 0, 'id': 1023306, 'name': 'Angel Kong', 'entryListId': -1}, {'url': 'mailto:[email protected]', 'type': 0, 'id': 1023289, 'name': 'Alex Do', 'entryListId': -1} |
[{'url': 'mailto:[email protected]', 'type': 0, 'id': 1021857, 'name': 'Ray Chan', 'entryListId': -1}, {'url': 'mailto:[email protected]', 'type': 0, 'id': 1023306, 'name': 'Paul Jones, 'entryListId': -1}] |
nan |
nan |
[{'url': 'mailto:[email protected]', '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!
CodePudding user response:
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
CodePudding user response:
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