PythonDataScienceHandbook icon indicating copy to clipboard operation
PythonDataScienceHandbook copied to clipboard

How to convert Json file into Python in tabular form.

Open preciserocks opened this issue 4 years ago • 2 comments

preciserocks avatar Sep 17 '20 06:09 preciserocks

Create A DataFrame from dictionary

import pandas as pd
data = [{'name': 'abc', 'age': 25}, {'name': 'xyz', 'age': 30}]
df = pd.DataFrame.from_dict(data, orient='columns')
df
Out[1]: 
  name  age
0  abc   25
1  xyz   30

If the Dictionary is nested you first need to normalize it

from pandas import json_normalize
data = [
  {
    'name': {
      'first': 'abc',
      'last': 'xyt'
    },
    'age': 25
  },
  {
    'name': {
      'first': 'def',
      'last': 'pqr'
    },
    'age': 30
  }
]
df = pd.DataFrame.from_dict(json_normalize(data), orient='columns')
df
Out[2]: 
   age name.first name.last
0   25        abc       xyt
1   30        def       pqr

soaibsafi avatar Sep 19 '20 08:09 soaibsafi

you had made data in listed form ?? and inside it you took dictionary??if you want to make table then do it with help o fdata structure , and use connector

coderSHeE avatar Nov 26 '21 09:11 coderSHeE