我一直在嘗試使用 Python 從 API 中獲取 Json 資料,以便可以將該資料傳輸到 sqlite3 資料庫。問題是資料不平衡。我的最終目標是將此 json 資料傳輸到 sqlite3 中的 .db 檔案。這是我所做的:
import pandas as pd
url = "https://baseballsavant.mlb.com/gf?game_pk=635886"
df = pd.read_json(url)
print(df)
這是我得到的錯誤:
raise ValueError("All arrays must be of the same length")
ValueError: All arrays must be of the same length
uj5u.com熱心網友回復:
您希望最終的 DataFrame 看起來像什么并不明顯,但是在這種情況下附加“orient='index'”可以避免出現問題。
import pandas as pd
url = "https://baseballsavant.mlb.com/gf?game_pk=635886"
df = pd.read_json(url, orient='index')
print(df)
您還可以使用請求模塊請求資料,并在將其加載到 DataFrame 之前對其進行準備
import requests
url = "https://baseballsavant.mlb.com/gf?game_pk=635886"
response = requests.get(url)
data = response.json()
"""
Do data transformations here
"""
df = pd.DataFrame.from_dict(data)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460534.html
