0

I have a Json array which has key value pairs like below.

[
    [
        {
            "value": "Dimension1",
            "formattedValue": "Dimension1"
        },      
        {
            "value": "Amount1",
            "formattedValue": "Amount1"
        },      
        {
            "value": "17253512.430000003",
            "formattedValue": "17,253,512.43"
        }
    ],
    [
        {
            "value": "Dimension1",
            "formattedValue": "Dimension1"
        },      
        {
            "value": "Amount2",
            "formattedValue": "Amount2"
        },      
        {
            "value": "-143721131.88999993",
            "formattedValue": "-143,721,131.89"
        }
    ],
    [
        {
            "value": "Dimension1",
            "formattedValue": "Dimension1"
        },      
        {
            "value": "Amount3",
            "formattedValue": "Amount3"
        },      
        {
            "value": "160974644.31999999",
            "formattedValue": "160,974,644.32"
        }
    ]   
]

I wanted to convert this into dataframe output like below. How can I achieve this please?

Dimension Amount1 Amount2 Amount3
Dimension1 17,253,512.43 -143,721,131.89 160,974,644.32

2 Answers 2

2

Since you are interested in 'formattedValue' so try:

df=(pd.DataFrame(data).stack()
      .map(lambda x:x.get('formattedValue'))
      .unstack().pivot(0,1,2)
      .rename_axis(columns=None,index='Dimension').reset_index())

Note: here data variable is your json data

output of df:

    Dimension   Amount1         Amount2             Amount3
0   Dimension1  17,253,512.43   -143,721,131.89     160,974,644.32
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It worked as expected for my Json
1

Try this


import pandas as pd  


data = 'your_json.json'
with open(data) as trainer:
    trainee= json.load(trainer)
df = pd.json_normalize(data['results'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.