【问题标题】:How to convert multi-row JSON file to Dataframe如何将多行 JSON 文件转换为 Dataframe
【发布时间】:2020-01-13 00:36:06
【问题描述】:

我正在使用输出多行 JSON 文件的 Instagram 抓取工具,我想从该文件中选择某些值并将它们分配给 DataFrame。

当我尝试使用 panda 的 pd.read_json 时,它只会将第一级保存到每个数据帧。

例如,我想要一个数据框,其第一行包含(括号中的 JSON 变量):

  • 赞(“edge_media_preview_like”:{“count”:1356...)

  • 评论数(“edge_media_to_comment”:{“count”:44)

JSON 文件如下所示:

{
    "GraphImages": [
        {
            "__typename": "GraphImage",
            "comments_disabled": false,
            "dimensions": {
                "height": 770,
                "width": 1080
            },
            "display_url": "https:abc123.com",
            "edge_media_preview_like": {
                "count": 1356
            },
            "edge_media_to_caption": {
                "edges": [
                    {
                        "node": {
                            "text": "TEXT EXAMPLE 123"
                        }
                    }
                ]
            },
            "edge_media_to_comment": {
                "count": 44
            },
            "gating_info": null,
            "id": "2219687023504340370",
            "is_video": false,
            "media_preview": "abc123media",
            "owner": {
                "id": "212343915"
            },
            "shortcode": "B7N6ZZkhTWS",
            "tags": [],
            "taken_at_timestamp": 1578827334,
            "thumbnail_resources": [
                {
                    "config_height": 150,
                    "config_width": 150,
                    "src": "abc123.com"
                },
                {
                    "config_height": 240,
                    "config_width": 240,
                    "src": "abc123.com"
                },
                {
                    "config_height": 320,
                    "config_width": 320,
                    "src": "https://abc123.com"
                },
                {
                    "config_height": 480,
                    "config_width": 480,
                    "src": "https:/abc123.com"
                },
                {
                    "config_height": 640,
                    "config_width": 640,
                    "src": "https://abc123.com"
                }
            ],
            "thumbnail_src": "https://abc123.com",
            "urls": [
                "https://abc123.com"
            ],
            "username": "abc123"
        }
    ]
}

我正在寻找:

    ImageNumber Likes   CommentCount
0   1           1356    44
1   ...         ...     ...

谢谢!

使用 pd.read_json 时添加错误结果:

    GraphImages
0   {'__typename': 'GraphImage', 'comments_disable...
1   {'__typename': 'GraphImage', 'comments_disable...
2   {'__typename': 'GraphImage', 'comments_disable...
3   {'__typename': 'GraphImage', 'comments_disable...

【问题讨论】:

  • .read_json() 可以正常工作,但您是否尝试过“手动”操作?
  • @AMC 感谢您的快速回答,但我不确定如何在具有这么多变量的 JSON 上手动完成
  • 你举了第一行的例子,你需要的数据就这些吗?
  • @AMC 手动是指收集数据的 Instagram 部分吗?不确定我是否关注
  • 手动是指收集数据的 Instagram 部分?哦,不,我只是指手动,而不是让 .read_json() 处理所有事情。

标签: python json pandas dataframe


【解决方案1】:

以下应该可以工作,

import json
with open('ig.json') as json_file:
    dct = json.load(json_file)

df = pd.io.json.json_normalize(dct, record_path="GraphImages")[["edge_media_preview_like.count", "edge_media_to_comment.count"]].rename({"edge_media_preview_like.count":"Likes", "edge_media_to_comment.count": "CommentCount"}, axis=1)
df["ImageNumber"] = df.index + 1

哪个产生,


    Likes   CommentCount    ImageNumber
0   1356    44              1

我不确定ImageNumber 的来源。但我认为这是GraphImages 中出现的项目顺序。如果是这样,df.Index + 1 会给你。

【讨论】:

  • 嗨@thushv89,感谢您的回复,我收到以下错误:“TypeError:字符串索引必须是整数”如果这有什么不同,文件的名称也是 ig.json!
  • @Gorlomi,您是否将 json 加载为字典?我已经编辑了我的答案以反映完整的解决方案。
【解决方案2】:

我找到了答案。原来,这个 instagram-scraper 输出了一个 JSON 文件,该文件由一个字典、一个列表、一个字典组成。提取代码如下:

import json
import pandas as pd

with open('ig.json') as json_file:
    data = json.load(json_file)

data['Likes'] = data['GraphImages'][0]['edge_media_preview_like']['count']
...

希望对以后的人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-10
    • 2018-03-11
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2022-11-06
    相关资源
    最近更新 更多