- 用法:- pandas.read_json(path_or_buf=None, orient=None, typ='frame', dtype=None, convert_axes=None, convert_dates=True, keep_default_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, encoding_errors='strict', lines=False, chunksize=None, compression='infer', nrows=None, storage_options=None)
- 將 JSON 字符串轉換為 pandas 對象。 - path_or_buf:有效的 JSON str、路徑對象或 file-like 對象
- 任何有效的字符串路徑都是可接受的。該字符串可以是一個 URL。有效的 URL 方案包括 http、ftp、s3 和文件。對於文件 URL,需要一個主機。本地文件可以是: - file://localhost/path/to/table.json。- 如果你想傳入一個路徑對象,pandas 接受任何 - os.PathLike。- 通過file-like 對象,我們指的是具有 - read()方法的對象,例如文件句柄(例如通過內置- open函數)或- StringIO。
- orient:str
- 指示預期的 JSON 字符串格式。 - to_json()可以生成兼容的 JSON 字符串,並帶有相應的 orient 值。可能的方向集是:- 'split': 像- {index -> [index], columns -> [columns], data -> [values]}這樣的字典
- 'records': 像- [{column -> value}, ... , {column -> value}]這樣的列表
- 'index': 像- {index -> {column -> value}}這樣的字典
- 'columns': 像- {column -> {index -> value}}這樣的字典
- 'values':隻是值數組
 - 允許值和默認值取決於 - typ參數的值。- 當 - typ == 'series',- 允許的方向是 - {'split','records','index'}
- 默認為 - 'index'
- Series 索引對於 orient - 'index'必須是唯一的。
 
- 當 - typ == 'frame',- 允許的方向是 - {'split','records','index', 'columns','values', 'table'}
- 默認為 - 'columns'
- DataFrame 索引對於方向 - 'index'和- 'columns'必須是唯一的。
- 對於方位 - 'index'、- 'columns'和- 'records',DataFrame 列必須是唯一的。
 
 
- typ:{‘frame’, ‘series’},默認 ‘frame’
- 要恢複的對象的類型。 
- dtype:布爾或字典,默認無
- 如果為真,則推斷數據類型;如果要 dtype 列的字典,則使用那些;如果為 False,則根本不推斷 dtypes,僅適用於數據。 - 對於除 - 'table'之外的所有- orient值,默認值為 True。
- convert_axes:布爾值,默認無
- 嘗試將軸轉換為正確的 dtypes。 - 對於除 - 'table'之外的所有- orient值,默認值為 True。
- convert_dates:bool 或 str 列表,默認 True
- 如果為 True,則可以轉換默認的 datelike 列(取決於keep_default_dates)。如果為 False,則不會轉換任何日期。如果是列名列表,那麽這些列將被轉換,並且默認的類似日期的列也可能被轉換(取決於keep_default_dates)。 
- keep_default_dates:布爾值,默認為真
- 如果解析日期(convert_dates 不是 False),則嘗試解析默認的 datelike 列。列標簽是 datelike 如果 - 它以 - '_at'結尾,
- 它以 - '_time'結尾,
- 它以 - 'timestamp'開頭,
- 它是 - 'modified',或
- 它是 - 'date'。
 
- numpy:布爾值,默認為 False
- 直接解碼為 numpy 數組。僅支持數字數據,但支持非數字列和索引標簽。另請注意,如果 numpy=True,則每個術語的 JSON 排序必須相同。 
- precise_float:布爾值,默認為 False
- 設置為在將字符串解碼為雙精度值時啟用更高精度 (strtod) 函數的使用。默認 (False) 是使用快速但不太精確的內置函數。 
- date_unit:str,默認無
- 檢測是否轉換日期的時間戳單位。默認行為是嘗試檢測正確的精度,但如果不需要,則傳遞 ‘s’, ‘ms’, ‘us’ or ‘ns’ 之一以強製分別僅解析秒、毫秒、微秒或納秒。 
- encoding:str,默認為“utf-8”
- 用於解碼 py3 字節的編碼。 
- encoding_errors:str,可選,默認 “strict”
- 如何處理編碼錯誤。 List of possible values 。 
- lines:布爾值,默認為 False
- 將文件作為每行的 json 對象讀取。 
- chunksize:int 可選
- 返回JsonReader 對象進行迭代。有關 - chunksize的更多信息,請參閱 line-delimited json docs。隻有在- lines=True時才能通過。如果這是無,文件將一次全部讀入內存。
- compression:str 或 dict,默認 ‘infer’
- 用於on-disk 數據的即時解壓縮。如果 ‘infer’ 和 ‘path_or_buf’ 是 path-like,則從以下擴展名檢測壓縮:“.gz”、“.bz2”、“.zip”、“.xz”或“.zst”(否則不壓縮)。如果使用‘zip’,ZIP 文件必須隻包含一個要讀入的數據文件。設置為 - None不解壓縮。也可以是鍵- 'method'設置為 {- 'zip'、- 'gzip'、- 'bz2'、- 'zstd'} 之一的字典,其他鍵值對被轉發到- zipfile.ZipFile、- gzip.GzipFile、- bz2.BZ2File或- zstandard.ZstdDecompressor,分別。例如,可以使用自定義壓縮字典為 Zstandard 解壓縮傳遞以下內容:- compression={'method': 'zstd', 'dict_data': my_compression_dict}。
- nrows:int 可選
- line-delimited jsonfile 中必須讀取的行數。隻有在 - lines=True時才能通過。如果這是 None,則將返回所有行。
- storage_options:字典,可選
- 對特定存儲連接有意義的額外選項,例如主機、端口、用戶名、密碼等。對於 HTTP(S) URL,鍵值對作為標頭選項轉發到 - urllib。對於其他 URL(例如以 “s3://” 和 “gcs://” 開頭),鍵值對被轉發到- fsspec。有關詳細信息,請參閱- fsspec和- urllib。
 
- 係列或DataFrame
- 返回的類型取決於 - typ的值。
 
 - 參數:- 返回:- 注意:- 特定於 - orient='table',如果帶有文字- Index名稱為- index的- DataFrame被寫入- to_json(),則後續讀取操作將錯誤地將- Index名稱設置為- None。這是因為- DataFrame.to_json()也使用- index來表示缺少的- Index名稱,後續的- read_json()操作無法區分兩者。- MultiIndex和任何以- 'level_'開頭的名稱都會遇到相同的限製。- 例子:- >>> df = pd.DataFrame([['a', 'b'], ['c', 'd']], ... index=['row 1', 'row 2'], ... columns=['col 1', 'col 2'])- 使用 - 'split'格式的 JSON 編碼/解碼數據幀:- >>> df.to_json(orient='split') '{"columns":["col 1","col 2"],"index":["row 1","row 2"],"data":[["a","b"],["c","d"]]}' >>> pd.read_json(_, orient='split') col 1 col 2 row 1 a b row 2 c d- 使用 - 'index'格式的 JSON 編碼/解碼數據幀:- >>> df.to_json(orient='index') '{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'- >>> pd.read_json(_, orient='index') col 1 col 2 row 1 a b row 2 c d- 使用 - 'records'格式的 JSON 編碼/解碼數據幀。請注意,此編碼不會保留索引標簽。- >>> df.to_json(orient='records') '[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]' >>> pd.read_json(_, orient='records') col 1 col 2 0 a b 1 c d- 使用表模式編碼 - >>> df.to_json(orient='table') '{"schema":{"fields":[{"name":"index","type":"string"},{"name":"col 1","type":"string"},{"name":"col 2","type":"string"}],"primaryKey":["index"],"pandas_version":"1.4.0"},"data":[{"index":"row 1","col 1":"a","col 2":"b"},{"index":"row 2","col 1":"c","col 2":"d"}]}'
相關用法
- Python pandas.read_pickle用法及代碼示例
- Python pandas.read_hdf用法及代碼示例
- Python pandas.read_xml用法及代碼示例
- Python pandas.read_table用法及代碼示例
- Python pandas.read_sql_table用法及代碼示例
- Python pandas.read_excel用法及代碼示例
- Python pandas.read_fwf用法及代碼示例
- Python pandas.read_stata用法及代碼示例
- Python pandas.read_sql用法及代碼示例
- Python pandas.read_csv用法及代碼示例
- Python pandas.read_html用法及代碼示例
- Python pandas.arrays.IntervalArray.is_empty用法及代碼示例
- Python pandas.DataFrame.ewm用法及代碼示例
- Python pandas.api.types.is_timedelta64_ns_dtype用法及代碼示例
- Python pandas.DataFrame.dot用法及代碼示例
- Python pandas.DataFrame.apply用法及代碼示例
- Python pandas.DataFrame.combine_first用法及代碼示例
- Python pandas.Index.value_counts用法及代碼示例
- Python pandas.DatetimeTZDtype用法及代碼示例
- Python pandas.DataFrame.cumsum用法及代碼示例
注:本文由純淨天空篩選整理自pandas.pydata.org大神的英文原創作品 pandas.read_json。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
