将深层嵌套的json从facebook转换为python中的dataframe

前端之家收集整理的这篇文章主要介绍了将深层嵌套的json从facebook转换为python中的dataframe前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试获取有关Facebook帖子的评论评论用户详细信息.我正在使用python facebook-sdk包.代码如下.

import facebook as fi
import json

graph = fi.GraphAPI('Access Token')
data = json.dumps(graph.get_object('DSIfootcandy/posts'))

从上面,我得到一个高度嵌套的json.在这里,我将只为fb中的一个帖子添加一个json字符串.

{
  "paging": {
    "next": "https://graph.facebook.com/v2.0/425073257683630/posts?access_token=&limit=25&until=1449201121&__paging_token=enc_AdD0DL6sN3aDZCwfYY25rJLW9IZBZCLM1QfX0venal6rpjUNvAWZBOoxTjbOYZAaFiBImzMqiv149HPH5FBJFo0nSVOPqUy78S0YvwZDZD","prevIoUs": "https://graph.facebook.com/v2.0/425073257683630/posts?since=1450843741&access_token=&limit=25&__paging_token=enc_AdCYobFJpcNavx6STzfPFyFe6eQQxRhkObwl2EdulwL7mjbnIETve7sJZCPMwVm7lu7yZA5FoY5Q4sprlQezF4AlGfZCWALClAZDZD&__prevIoUs=1"
  },"data": [
    {
      "picture": "https://fbcdn-photos-e-a.akamaihd.net/hphotos-ak-xfa1/v/t1.0-0/p130x130/1285_5066979392443_n.png?oh=b37a42ee58654f08af5abbd4f52b1ace&oe=570898E7&__gda__=1461440649_aa94b9ec60f22004675c4a527e8893f","is_hidden": false,"likes": {
        "paging": {
          "cursors": {
            "after": "MTU3NzQxODMzNTg0NDcwNQ==","before": "MTU5Mzc1MjA3NDE4ODgwMA=="
          }
        },"data": [
          {
            "id": "1593752074188800","name": "Maduri Priyadarshani"
          },{
            "id": "427605680763414","name": "Darshi Mashika"
          },{
            "id": "599793563453832","name": "Shakeer Nimeshani Shashikala"
          },{
            "id": "1577418335844705","name": "Däzlling Jalali Muishu"
          }
        ]
      },"from": {
        "category": "Retail and Consumer Merchandise","name": "Footcandy","category_list": [
          {
            "id": "2239","name": "Retail and Consumer Merchandise"
          }
        ],"id": "425073257683630"
      },"name": "Timeline Photos","privacy": {
        "allow": "","deny": "","friends": "","description": "","value": ""
      },"is_expired": false,"comments": {
        "paging": {
          "cursors": {
            "after": "WTI5dGJXVnVkRjlqZFhKemIzSUVXdNVFExTURRd09qRTBOVEE0TkRRNE5EVT0=","before": "WTI5dGJXVnVkRjlqZFhKemIzNE16Y3dNVFExTVRFNE9qRTBOVEE0TkRRME5UVT0="
          }
        },"data": [
          {
            "from": {
              "name": "NiFû Shafrà","id": "1025030640553"
            },"like_count": 0,"can_remove": false,"created_time": "2015-12-23T04:20:55+0000","message": "wow lovely one","id": "50018692683829_500458145118","user_likes": false
          },{
            "from": {
              "name": "Shamnaz Lukmanjee","id": "160625809961884"
            },"created_time": "2015-12-23T04:27:25+0000","message": "Nice","id": "500186926838929_500450145040","user_likes": false
          }
        ]
      },"actions": [
        {
          "link": "https://www.facebook.com/425073257683630/posts/5001866838929","name": "Comment"
        },{
          "link": "https://www.facebook.com/42507683630/posts/500186926838929","name": "Like"
        }
      ],"updated_time": "2015-12-23T04:27:25+0000","link": "https://www.facebook.com/DSIFootcandy/photos/a.438926536298302.1073741827.4250732576630/50086926838929/?type=3","object_id": "50018692838929","shares": {
        "count": 3
      },"created_time": "2015-12-23T04:09:01+0000","message": "Reach new heights in the cute and extremely comfortable \"Silviar\" www.focandy.lk","type": "photo","id": "425077683630_50018926838929","status_type": "added_photos","icon": "https://www.facebook.com/images/icons/photo1.gif"
    }
  ]
}

现在我需要将这些数据放入数据帧中,如下所示(无需全部获取).

item | Like_id |Like_username | comments_userid |comments_username|comment(msg)|
-----+---------+--------------+-----------------+-----------------+------------+
Bag  |   45546 |  noel        | 641             | James           | nice work  |
-----+---------+--------------+-----------------+-----------------+------------+

任何帮助都将得到高度赞赏.

最佳答案
不完全像您的预期格式,但这是一个解决方案的制作:

import pandas

DictionaryObject_as_List = str(mydict).replace("{","").replace("}","").replace("[","").replace("]","").split(",")

newlist = []

for row in DictionaryObject_as_List :
    row = row.replace('https://',' ').split(":")
    exec('newlist.append ( ' + "[" + ",".join(row)+"]" + ')')

DataFrame_Object = pandas.DataFrame(newlist)

print DataFrame_Object
原文链接:https://www.f2er.com/python/438464.html

猜你在找的Python相关文章