Nested Json to pandas DataFrame with specific format

后端 未结 1 1878
别那么骄傲
别那么骄傲 2020-12-13 09:00

i need to format the contents of a Json file in a certain format in a pandas DataFrame so that i can run pandassql to transform the data and run it through a scoring model.<

相关标签:
1条回答
  • 2020-12-13 09:48

    If you load in the entire json as a dict (or list) e.g. using json.load, you can use json_normalize:

    In [11]: d = {"response": {"body": {"contact": {"email": "mr@abc.com", "mobile_number": "0123456789"}, "personal": {"last_name": "Muster", "gender": "m", "first_name": "Max", "dob": "1985-12-23", "family_status": "single", "title": "Dr."}, "customer": {"verified": "true", "customer_id": "1234567"}}, "token": "dsfgf", "version": "1.1"}}
    
    In [12]: df = pd.json_normalize(d)
    
    In [13]: df.columns = df.columns.map(lambda x: x.split(".")[-1])
    
    In [14]: df
    Out[14]:
            email mobile_number customer_id verified         dob family_status first_name gender last_name title  token version
    0  mr@abc.com    0123456789     1234567     true  1985-12-23        single        Max      m    Muster   Dr.  dsfgf     1.1
    
    0 讨论(0)
提交回复
热议问题