[英]Is there a way to prevent Pandas read_json (orient='split') from opportunistically converting a float64 column to int64?
[英]Pandas read_json with int64 values raises ValueError: Value is too big
我正在嘗試將 json 文件讀入數據幀。
df = pd.read_json('test.log', lines=True)
但是有一些值是 int64 和 Pandas 提高:
ValueError: Value is too big
我嘗試將precise_float
設置為True
,但這並沒有解決它。
當我逐行執行時,它會起作用:
df = pd.DataFrame()
with open('test.log') as f:
for line in f:
data = json.loads(line)
df = df.append(data, ignore_index=True)
然而,這是非常緩慢的。 對於大約 50k 行的文件,它需要很長時間。
有沒有辦法可以將某些列的值設置為使用 int64?
After updating pandas to a newer version (tested with 1.0.3), this workaround by artdgn can be applied to overwrite the loads()
function in pandas.io.json._json
, which is ultimately used when pd.read_json()
is called .
如果上面的鏈接停止工作,請復制解決方法:
import pandas as pd
# monkeypatch using standard python json module
import json
pd.io.json._json.loads = lambda s, *a, **kw: json.loads(s)
# monkeypatch using faster simplejson module
import simplejson
pd.io.json._json.loads = lambda s, *a, **kw: simplejson.loads(s)
# normalising (unnesting) at the same time (for nested jsons)
pd.io.json._json.loads = lambda s, *a, **kw: pandas.json_normalize(simplejson.loads(s))
在使用 artdgn 描述的 3 種方法中的一種覆蓋loads()
function 后, read_json()
也適用於int64
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.