PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 25, 2022

[FIXED] how to convert nested dict to dataframe using python?

 July 25, 2022     dataframe, dictionary, json, pandas, python     No comments   

Issue

I need to convert this dict to dataframe/csv

data= {
  "message": {
    "id": 474735,
    "token": "GI797jMv8FuG",
    "direction": "outgoing",
    "message_id": "t",
    "to": "user@email.com",
    "from": "user@email.com",
    "subject": "Test Message at July 05, 2022 16:10",
    "timestamp": 1657017652.063772,
    "spam_status": "NotChecked",
    "tag": null
  },
  "status": "Sent",
  "details": "Message for  accepted by aspmx.l.google.com (142.251.12.26) (from 49.248.200.108)",
  "output": "250 2.0.0 OK  1657017654 f1-20020a63de01000000b0040d4ea0274fsi11524255pgg.697 - gsmtp\n",
  "sent_with_ssl": false,
  "timestamp": 1657017654.6172404,
  "time": 1.26
}

I used this website https://www.convertcsv.com/json-to-csv.htm and I'm getting the output as I want but need the code to convert dict to dataframe so i can put this in my script please file the output table in the attachment

enter image description here


Solution

You need to create a dictionary with the default value of list and add each element in this dictionary.

import pandas as pd

tmp = {}
for k,v in data.items():
    if isinstance(v, dict):
        for a,b in v.items():
            tmp.setdefault(f'{k}/{a}', []).append(b)
    else:
        tmp.setdefault(k, []).append(v)
        
        
        
pd.DataFrame(tmp)

Output:

   message/id   message/token   message/direction   message/message_id  message/to  message/from    message/subject message/timestamp   message/spam_status message/tag status  details output  sent_with_ssl   timestamp   time
0   474735      GI797jMv8FuG    outgoing                     t   user@email.com user@email.com  Test Message at July 05, 2022 16:10 1.657018e+09    NotChecked  null    Sent    Message for accepted by aspmx.l.google.com (1...    250 2.0.0 OK 1657017654 f1-20020a63de01000000...    false   1.657018e+09    1.26


Answered By - I'mahdi
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing