我有一個熊貓系列,看起來像這樣:一堆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