1

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!

morf
  • 125
  • 11

2 Answers2

4

Let us try ChainMap

from collections import ChainMap
df['dicts']=df['celldata'].map(lambda x : dict(ChainMap(*x)))
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Looks like it's working!, but what kind of black magic it is and why I've never heard of ChainMap :) – morf May 14 '20 at 13:25
  • @morf aha ~ https://stackoverflow.com/questions/23392976/what-is-the-purpose-of-collections-chainmap – BENY May 14 '20 at 13:27
  • @YORBEN_S thanks, it seems to be simplest approach. It's a pitty I haven't heard of ChainMap before. My bad! – morf May 14 '20 at 13:36
0

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':...  
JQadrad
  • 541
  • 2
  • 16