簡體   English   中英

Pandas 'read_json' 沒有按預期工作

[英]Pandas 'read_json' not working as expected

我想用 pandas 加載 JSON 文件,但它沒有像我預期的那樣工作! 我已經提到了這個stackoverflow 答案,但我的問題不是那個。 JSON 文件如下所示:

JSON 文件

加載文件的代碼:-

import pandas as pd
df = pd.read_json("BrowserHistory.json")
print(df)

Output:-

Output Pandas Dataframe

但我不希望只有 1 列包含每個 json 元素。 我想要 6 列,即 'favicon_url'、'page_transition'、'title'、'url'、'client_id' 和 'time_usec',在上面的 'json 文件'照片中描述,然后每列應該包含它在每個元素。

像這樣:

favicon url   page_transition   title   url   client_id   time_user
    .                .            .      .        .           .
    .                .            .      .        .           .
    .                .            .      .        .           .
    .                .            .      .        .           .

JSON 文件:

{
    "Browser History": [
        {
            "favicon_url": "https://www.google.com/favicon.ico",
            "page_transition": "LINK",
            "title": "Google Takeout",
            "url": "https://takeout.google.com/",
            "client_id": "cliendid",
            "time_usec": 1620386529857946
},
        {
            "favicon_url": "https://www.google.com/favicon.ico",
            "page_transition": "LINK",
            "title": "Google Takeout",
            "url": "https://takeout.google.com/",
            "client_id": "cliendid",
            "time_usec": 1620386514845201
},
        {
            "favicon_url": "https://www.google.com/favicon.ico",
            "page_transition": "LINK",
            "title": "Google Takeout",
            "url": "https://takeout.google.com/",
            "client_id": "cliendid",
            "time_usec": 1620386499014063
},
        {
            "favicon_url": "https://ssl.gstatic.com/ui/v1/icons/mail/rfr/gmail.ico",
            "page_transition": "LINK",
            "title": "Gmail",
            "url": "https://mail.google.com/mail/u/0/#inbox",
            "client_id": "cliendid",
            "time_usec": 1620386492788783
}
  ]
}

問題是因為文件周圍的{} ,pandas 認為 JSON 的第一級是列,因此它僅使用瀏覽器歷史記錄作為列。 您可以使用此代碼來解決您的問題:

import pandas as pd
df = pd.DataFrame(json.load(open('BrowserHistory.json', encoding='cp850'))['Browser History'])
print(df)

由於您的對象位於 JSON 的第二級列表中,因此您無法使用read_json將其直接讀入 dataframe 中。 相反,您可以將 json 讀入變量,然后從中創建 dataframe:

import pandas as pd
import json

f = open("BrowserHistory.json")
js = json.load(f)
df = pd.DataFrame(js['Browser History'])
df
#                                          favicon_url page_transition  ... client_id         time_usec
# 0                 https://www.google.com/favicon.ico            LINK  ...  cliendid  1620386529857946
# 1                 https://www.google.com/favicon.ico            LINK  ...  cliendid  1620386514845201
# 2                 https://www.google.com/favicon.ico            LINK  ...  cliendid  1620386499014063
# 3  https://ssl.gstatic.com/ui/v1/icons/mail/rfr/g...            LINK  ...  cliendid  1620386492788783

請注意,您可能需要在open調用中指定文件編碼,例如

f = open("BrowserHistory.json", encoding="utf8")

暫無
暫無

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

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