1

The following is the json data available to me

{
  "status": "success",
  "message": "Transactions Details",
  "TxnArray": [    
    {
      "transactionAmount": {"0": 3500},
      "createdAt": {"0": "17/04/2020"}
    },    
    {
      "transactionAmount": {"1": 4500},
      "createdAt": {"1": "19/04/2020"}
    }
  ]
}

Want to convert the above data in pandas Dataframe like

      transactionAmount createdAt
    0 3500               17/4/2020
    1 4500               19/4/2020

3 Answers 3

1

A slightly different yet compact approach,

pd.concat(pd.DataFrame.from_dict(d) for d in data['TxnArray'])
Sign up to request clarification or add additional context in comments.

Comments

1

A more compact approach:

import pandas as pd

df = pd.DataFrame([
    {k: v[str(i)] for k, v in list_item.items()} for i, list_item in enumerate(json_data['TxnArray'])
])

3 Comments

That was really helpful and compact solution. Thank you so much..
I am getting started with all this. Could you help me what "_" is used for? It would be a great help.
you have a point @PriteshChoksi . _ is not human friendly. I edited my answer in order to increase readability. So... _ was every list's item
0
import pandas as pd
import json

json_str = json.loads('''{
  "status": "success",
  "message": "Transactions Details",
  "TxnArray": [    
    {
      "transactionAmount": {"0": 3500},
      "createdAt": {"0": "17/04/2020"}
    },    
    {
      "transactionAmount": {"1": 4500},
      "createdAt": {"1": "19/04/2020"}
    }
  ]
}''')

df = pd.DataFrame(json_str['TxnArray'])

enter image description here

def clean_function(x):
    return list(x.values())[0]

df = df.applymap(lambda x: clean_function(x))

enter image description here

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.