0

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...

El_1988
  • 339
  • 3
  • 13
  • 2
    That is not possible, you can't have a dictionary where the same key appears twice with different values. – mkrieger1 Jul 24 '20 at 00:13
  • In general, this should answer your question: [How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?](https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression-in-python-taking-union-o) – mkrieger1 Jul 24 '20 at 00:16
  • P.S. Perhaps you should pick smaller numbers like 1, 2, 3, 4 to demonstrate with. – Mateen Ulhaq Jul 24 '20 at 00:17
  • In earlier Python versions, `dict.items()` returned a list, which you could concatenate using the `+` operator. Presumably you were following some advice which was only valid for an older Python version. – mkrieger1 Jul 24 '20 at 00:17
  • @mkrieger1 I saw this in another post that it is not possible, so then how is it If I type in my desired output, and query the 'type' , it says 'dict' ? will show example in edited question. – El_1988 Jul 24 '20 at 00:23

1 Answers1

1
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.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • thanks for your response, so the basic idea is that there is no way to create in python the dataset in the shape I'm looking for. Aim for this is: A model ingests data that looks like this, but has not been provided with documentation, so I need to reverse identify how its input (my desired output) been created from a base panda dataframe. The data when converted from dataframe to nested dictionary looks like what I've shared as desired output. Thus my confusion. – El_1988 Jul 24 '20 at 00:32