创建太长整数的数据帧

时间:2017-11-14 03:33:56

标签: python pandas

我正在尝试创建应包含非常长整数的数据帧。我的代码如下:

import pandas as pd

data = [123456789123456789123456789123456789123456789, 
123456789123456789123456789123456789123456789]
cols = ['Col1', 'Col2']
df = pd.DataFrame(data, columns=cols)
print(df)

但是我收到以下错误:

Traceback (most recent call last):
File "/home/watson/Desktop/test.py", line 5, in <module>
    df = pd.DataFrame(data, columns=cols)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", 
line 330, in __init__
    copy=copy)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", 
line 463, in _init_ndarray
    values = _prep_ndarray(values, copy=copy)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", 
line 5578, in _prep_ndarray
    values = convert(values)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", 
line 5567, in convert
    return maybe_convert_platform(v)
File "/usr/local/lib/python2.7/dist-
packages/pandas/core/dtypes/cast.py", line 47, in 
maybe_convert_platform
    values = lib.maybe_convert_objects(values)
File "pandas/_libs/src/inference.pyx", line 1165, in 
pandas._libs.lib.maybe_convert_objects (pandas/_libs/lib.c:58498)
    OverflowError: long int too large to convert

1 个答案:

答案 0 :(得分:2)

您可以通过设置dtype=object来避免错误。此外,您的示例中有一个拼写错误,并且在您指定两列时传递错误的形状数据。以下行有效:

df = pd.DataFrame([data], columns=cols, dtype=object)

注意:我将数据包装在列表中以使其成为正确的尺寸。如果我不这样做,它会给OverflowError,即使它真的应该给出ValueError关于传递值的形状。

In [1]: df = pd.DataFrame([data], columns=cols, dtype=object)

In [2]: df
Out[2]:
                                            Col1  \
0  123456789123456789123456789123456789123456789

                                            Col2
0  123456789123456789123456789123456789123456789