TransWikia.com

How to merge dictionaries in np.ndarray into one dictionary?

Stack Overflow Asked by El_1988 on December 20, 2021

Following my question on Convert Dataframe to Nested Dictionary in Python, I have been trying to convert a Pandas dataframe into a nested dictionary.

Current challenge is merging multiple dictionaries inside a np.ndarray into one.

  1. My current output dkl is

    array([{0.7863340563991272: 0.0002639915522703274},
           {0.7863340563991272: 0.0006863780359028511}], dtype=object)
    

    Which when checked for type results numpy.ndarray.

  2. I need to get dkl to be

    {0.7863340563991272: 0.0002639915522703274,
     0.7863340563991272: 0.0006863780359028511}
    

I am failing to get the desired results. I tried to turn dkl into a l1 = list(dkl), resulting in:

[{0.7863340563991272: 0.0002639915522703274},
 {0.7863340563991272: 0.0006863780359028511}]

I am now able to get the two elements of the list above which are it1 = {0.7863340563991272: 0.0002639915522703274} and it2 = {0.7863340563991272: 0.0006863780359028511}, which I am trying to merge into one dictionary. I tried:

 dict(it1.items() + it2.items())

resulting in:

TypeError                                 Traceback (most recent call last)
<ipython-input-150-4422fd31b345> in <module>
----> 1 dict(it1.items() + it2.items())

TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'

In short, I need to get from 1 to 2.
Update: Ref to mention in comments that this shape of output is not possible in dict, I am tyring to understand how this comes about: enter image description here

Reason being that I have a dataset I need to replicate that is structured as shown above in my desired output, and if not possible with dict, I have no idea how it’s been created…

One Answer

In [10]: dkl=np.array([{0.7863340563991272: 0.0002639915522703274}, 
    ...:        {0.7863340563991272: 0.0006863780359028511}], dtype=object)                          
In [11]: dkl                                                                                         
Out[11]: 
array([{0.7863340563991272: 0.0002639915522703274},
       {0.7863340563991272: 0.0006863780359028511}], dtype=object)

An object dtype array is for (most) practical purposes a list. Your list(dkl) does that explicitly, or tolist():

In [12]: alist = dkl.tolist()                                                                        
In [13]: alist                                                                                       
Out[13]: 
[{0.7863340563991272: 0.0002639915522703274},
 {0.7863340563991272: 0.0006863780359028511}]

One way way to merge dictionaries is update:

In [14]: d1 = alist[0]                                                                               
In [15]: for d in alist[1:]: d1.update(d)                                                            
In [16]: d1                                                                                          
Out[16]: {0.7863340563991272: 0.0006863780359028511}

In your case both have the same key, so the result is last dictionary in the list.

{0.7863340563991272: 0.0002639915522703274,
 0.7863340563991272: 0.0006863780359028511}

is impossible since keys of a dictionary are unique.

Other possible mergers include adding values with matching keys, or a list with values. I'm not sure I should spend time on those since you don't seem to know what you really need.

You try adding the items; using the py3 list(items), we get a list of lists of tuples:

In [20]: [list(d.items()) for d in dkl]                                                              
Out[20]: 
[[(0.7863340563991272, 0.0002639915522703274)],
 [(0.7863340563991272, 0.0006863780359028511)]]

where the tuples contain the key and value. But apart from concatenating I don't know what you want to do with those tuples.

In sum, you have a bunch of dictionaries, which may or may not have matching keys. There's nothing special, numpy-wise here.

Answered by hpaulj on December 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP