pythonjsonpandas

convert pandas multiindex series to Json python


Hi I have two pandas series similar to below

PnL

           Product Name      Price
Company A  Orange            3000
Company B  Apple             2000
           Grapes            1000

Tax

           Product Name      Price
Company A  Orange            100
Company B  Apple             100
           Grapes            10

I would like to transform the pandas series into the following JSON format

{'PnL':{'Company A':{'productName':'Orange','price':3000},
        'Company B':[{'productName':'Apple','price':2000},
                     {'productName':'Grapes','price':1000}]
       },
 'Tax':{'Company A':{'productName':'Orange','price':100},
        'Company B':[{'productName':'Apple','price':100},
                     {'productName':'Grapes','price':10}]
       }
}

I have tried to use the code below

convertedJson = json.dumps([{'company': k[0], 'productName':k[1],'price': v} for k,v in df.items()])

but I cannot form the JSON which I want to produce. Thank you for your help


Solution

  • You can use concat for join DataFrames together and then groupby with to_dict for expected output:

    df = pd.concat([s1, s2], keys=('PnL','Tax')).reset_index()
    df.columns = ['type','company','productName','price']
    print (df)
      type    company productName  price
    0  PnL  Company A      Orange   3000
    1  PnL  Company B       Apple   2000
    2  PnL  Company B      Grapes   1000
    3  Tax  Company A      Orange   3000
    4  Tax  Company B       Apple   2000
    5  Tax  Company B      Grapes   1000
    
    d = (df.groupby(['type','company'])['productName','price']
           .apply(lambda x: x.to_dict('r'))
           .reset_index(name='data')
           .groupby('type')['company','data']
           .apply(lambda x: x.set_index('company')['data'].to_dict())
           .to_json()
           )
    

    print (d)
    
    {
        "PnL": {
            "Company A": [{
                "productName": "Orange",
                "price": 3000
            }],
            "Company B": [{
                "productName": "Apple",
                "price": 2000
            }, {
                "productName": "Grapes",
                "price": 1000
            }]
        },
        "Tax": {
            "Company A": [{
                "productName": "Orange",
                "price": 3000
            }],
            "Company B": [{
                "productName": "Apple",
                "price": 2000
            }, {
                "productName": "Grapes",
                "price": 1000
            }]
        }
    }