簡體   English   中英

如何將多行 JSON 文件轉換為 Dataframe

[英]How to convert multi-row JSON file to Dataframe

我正在使用輸出多行 JSON 文件的 instagram 抓取器,我想從該文件中選擇某些值並將它們分配給 DataFrame。

當我嘗試使用熊貓的 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...

以下應該工作,

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出現的項目GraphImages 如果是這樣, df.Index + 1會給你。

我找到了答案。 事實證明,這個 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']
...

我希望可以幫助未來的人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM