-1

I have 2 dicts which are as follows:

{
"states": 
         [
           {
             "status": "BV",  
             "median": 240.0 
            }, 
            {
             "status": "CORR", 
             "median": 720.0
            }, 
         ]
}   

and

{
"diseases": [
    {
        "status": "BV",  
        "median": 100.0,
        "disease_name": "Lupus"
    }, 
    {
        "status": "BV", 
        "median": 128.0,
        "disease_name": "Pulmonary Arterial Hypertension"
    }, 
    {
        "status": "CORR", 
        "median": 321.0,
        "disease_name": "Pulmonary Arterial Hypertension"
    }, 
    {
        "status": "CORR",
        "median": 670.0,
        "disease_name": "Rheumatology"
    }
]
}

How would one go about merging the 2 dicts into one using pandas so that the output is as follows:

{
"states": 
        [
            {
                "status": "BV",
                "median": 240.0, 
                "drilldown": "BV"
            }, 
            {
                "status": "CORR",
                "median": 720.0,
                "drilldown": "CORR"
            }
        ],

"drilldown": 
        [
          {
            "name":"BV",
            "data":
                    [
                      {
                        "median": 100.0,
                        "disease_name": "Lupus"
                      },
                      {
                        "median": 128.0, 
                        "disease_name": "Pulmonary Arterial Hypertension"
                      }
                    ]        
          },               
          {
            "name":"CORR",
            "data":
                    [
                      {
                        "median": 321.0,
                        "disease_name": "Lupus"
                      },
                      {
                        "median": 670.0, 
                        "disease_name": "Rheumatology"
                      }
                  ]        
            }
          ]
}

i did try giving it a shot by iterating over the lists but it looks very hacky and is computationally expensive.Was wondering if there is a better way to do it with Pandas ?

Amistad
  • 7,100
  • 13
  • 48
  • 75
  • why the downvote ? can i have a justification ?? – Amistad Sep 04 '15 at 10:08
  • It is much better to show your current code, instead of just showing input and output. This way you show that you do not intend to use stackoverflow as a code writing service and it is easier for us to understand the logic behind the transformation. – cel Sep 04 '15 at 10:22

2 Answers2

1

Pandas concat and merge functions are not built for working on dicts, they are built for DataFrame and Series etc.

You may find help in this existing question.

combined = {}
combined.update(d1)
combined.update(d2)
Community
  • 1
  • 1
Alex
  • 5,364
  • 9
  • 54
  • 69
0

You can use concat([list of dict]) or merge([list of dict]) to get the desired result.

Navneet
  • 4,543
  • 1
  • 19
  • 29