簡體   English   中英

Int 太大而無法在執行 .astype(int) 時轉換為 C long

[英]Int too large to convert to C long while doing .astype(int)

我正在運行一個 python 腳本,我必須使用astype(int)方法將字符串列從熊貓df轉換為int 但是,我收到以下錯誤:

"Python int too large to convert to C long"

我的字符串都是字符串格式的數字,最多15個字符。 有沒有辦法將此列轉換為 int 類型而不會彈出此錯誤?

你需要使用 .astype('int64')

import pandas as pd
df = pd.DataFrame({'test': ['999999999999999','111111111111111']})
df['int'] = df['test'].astype('int64')

檢查您是否將大整數值分配給變量。

1) 不使用乘法,而是使用冪

例如:

x = [10**i for i in range(1,1000)] instead of [10*i for i in range(1,1000)]  ,which will cross the variable limit 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM