python - 将 pandas 数据帧导出到 JSON 时如何解决 OverflowError

标签 python json pandas dataframe jupyter

在 Jupyter 中,我有一个包含 400 000 个对象的数据框,我无法在不遇到以下错误的情况下将其完全导出到 JSON 文件。

只要我将导出限制为前 141 000 个对象,无论这些第一个对象的顺序如何,导出都很好。

我应该注意处理大型 JSON 文件的任何大小限制吗? 谢谢。

OverflowError                             Traceback (most recent call last)
<ipython-input-254-b59373f1eeb2> in <module>
----> 1 df4.to_json('test.json', orient = 'records')

~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in to_json(self, path_or_buf, orient, date_format, double_precision, force_ascii, date_unit, default_handler, lines, compression, index)
   1889                             default_handler=default_handler,
   1890                             lines=lines, compression=compression,
-> 1891                             index=index)
   1892 
   1893     def to_hdf(self, path_or_buf, key, **kwargs):

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/json.py in to_json(path_or_buf, obj, orient, date_format, double_precision, force_ascii, date_unit, default_handler, lines, compression, index)
     56         double_precision=double_precision, ensure_ascii=force_ascii,
     57         date_unit=date_unit, default_handler=default_handler,
---> 58         index=index).write()
     59 
     60     if lines:

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/json.py in write(self)
     99         return self._write(self.obj, self.orient, self.double_precision,
    100                            self.ensure_ascii, self.date_unit,
--> 101                            self.date_format == 'iso', self.default_handler)
    102 
    103     def _write(self, obj, orient, double_precision, ensure_ascii,

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/json.py in _write(self, obj, orient, double_precision, ensure_ascii, date_unit, iso_dates, default_handler)
    154                                                double_precision,
    155                                                ensure_ascii, date_unit,
--> 156                                                iso_dates, default_handler)
    157 
    158 

~/anaconda3/lib/python3.7/site-packages/pandas/io/json/json.py in _write(self, obj, orient, double_precision, ensure_ascii, date_unit, iso_dates, default_handler)
    110             date_unit=date_unit,
    111             iso_dates=iso_dates,
--> 112             default_handler=default_handler
    113         )
    114 

OverflowError: int too big to convert

最佳答案

试试下面的代码:

 df4.to_json('test.json',default_handler=str, orient = 'records')

default_handler 在对象无法以其他方式转换为适合 JSON 的格式时使用。

阅读documentation .

关于python - 将 pandas 数据帧导出到 JSON 时如何解决 OverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54678661/

相关文章:

python - 带有 cookie 的 HTTP POST 和 GET 用于在 python 中进行身份验证

Python - 在给定字符串中搜索连字符

javascript - 有没有办法创建与 JSON.stringify(jsObject) 创建的 Java <-> JSON 映射对象完全相同的字符串?

c# - 使用 C# 将 UTF-8 转换为 Unicode

python - 如何附加两个具有不同行数的 DataFrame,填充较短的一个

python - 将十六进制字符串转换为 bytes 函数的正确形式

json - 在 NodeJS 中使用 JSON 数据

python - 获取具有以模式开头的字符串的单元格的所有行和列位置

python - Pandas - 用特定组的平均值替换列中的 NaN

python - 当函数中有循环时将递归转换为迭代