我有一个熊猫系列,看起来像这样:一堆unicode
串熊猫数据类型转换问题
>>> some_id
0 400742773466599424
1 400740479161352192
2 398829879107809281
3 398823962966097921
4 398799036070653952
Name: some_id, dtype: object
我可以做以下的,但我失去的精度。
>>> some_id.convert_objects(convert_numeric=True)
0 4.007428e+17
1 4.007405e+17
2 3.988299e+17
3 3.988240e+17
4 3.987990e+17
Name: some_id, dtype: float64
但如果我这样做some_id.astype(int)
,我得到如下:ValueError: invalid literal for long() with base 10
我怎样才能将它们转换为int
或int64
类型,同时保持精度? 我正在使用熊猫0.16.2
更新:我发现了这个错误。 some_id.astype(int)
或任何其他形式的应该工作。沿着成千上万行的某处,some_id有string
的文本(不是字符串编号),所以它正在停止int64
转换。
感谢
如果你使用'some_id.astype(np.int64)'会发生什么? – dagrha
你使用的是什么版本的熊猫。鉴于上面的示例数据,我没有任何问题将some_id从对象转换为int(Pandas 0.14.1)。此外,您不会将精度转换为数字,它只是不显示。 – Alexander
@Alexander可能需要升级 - 0.16.2是当前版本:) – MattDMo