I'm struggling with simple lambda to convert list of nested dicts stored in df column but got stuck.
My df looks like
index synthkey celldata
0 870322681ffffff [{'3400_251': {'s': -77, 'q': -8}}, {'3400_426': {'s': -116, 'q': -16}}]
0 87032268effffff [{'3400_376': {'s': -97, 'q': -12}}, {'3400_426': {'s': -88, 'q': -12}}]
What I'd like to achieve is to have it like that:
index synthkey celldata
0 870322681ffffff {'3400_251': {'s': -77, 'q': -8},'3400_426': {'s': -116, 'q': -16}}
I've tried multiple attempts like:
df['dicts'] = df['celldata'].apply(lambda x: {}.update(*x))
or
df['dicts'] = df.apply(lambda x: {*x['celldata']})
but it got me nowhere near the solution.
Thanks!
Let us try ChainMap
from collections import ChainMap
df['dicts']=df['celldata'].map(lambda x : dict(ChainMap(*x)))
Using a simple for-loop to merge the dictionaries using merge_dict = {**dict_one, **dict_two}
.
df = pd.DataFrame([{
'index': 0,
'synthkey': '870322681ffffff',
'celldata': [{'3400_251': {'s': -77, 'q': -8}}, {'3400_426': {'s': -116, 'q': -16}}]
},{
'index': 0,
'synthkey': '87032268effffff',
'celldata': [{'3400_376': {'s': -97, 'q': -12}}, {'3400_426': {'s': -88, 'q': -12}}]
}])
def merge_dicts(list_of_dicts):
out = {}
for elem in list_of_dicts:
out = {**out, **elem}
return out
df['new'] = df['celldata'].apply(merge_dicts)
print(df.head())
# index synthkey celldata \
# 0 0 870322681ffffff [{'3400_251': {'s': -77, 'q': -8}}, {'3400_426...
# 1 0 87032268effffff [{'3400_376': {'s': -97, 'q': -12}}, {'3400_42...
# new
# 0 {'3400_251': {'s': -77, 'q': -8}, '3400_426': ...
# 1 {'3400_376': {'s': -97, 'q': -12}, '3400_426':...
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.