亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我在 python 腳本中收到整數太大而無法轉換為浮點數的錯誤?

為什么我在 python 腳本中收到整數太大而無法轉換為浮點數的錯誤?

白板的微信 2023-04-11 15:49:54
我是 python 的新手。我正在嘗試解決錯誤我有一個數據框(reprex)-import pandas as pd    df    Out[29]:             Id  ServiceSubCodeKey   PrintDate    0  1895650                  2  2018-07-27    1  1895650                  4  2018-08-13    2  1896355                  2  2018-08-10    3  1897675                  9  2018-08-13    4  1897843                  2  2018-08-10    5  2178737                  3  2019-06-14    6  2178737                  4  2019-06-14    7  2178737                  7  2019-06-14    8  2178737                  1  2019-06-14    9  2178750                699  2019-06-14columns = (    pd.get_dummies(df["ServiceSubCodeKey"])    .reindex(range(df.ServiceSubCodeKey.min(),        df.ServiceSubCodeKey.max()+1), axis=1, fill_value=0)    # now it has all digits    .astype(str)    )codes = pd.Series(    [int(''.join(row)) for row in columns.itertuples(index=False)],    index=df.index)codes = (    codes.groupby(df.Id).transform('sum').astype('str')    .str.pad(width=columns.shape[1], fillchar='0')    .str.rstrip('0') # this will remove trailing 0's    )print(codes)df = df.assign(one_hot_ssc=codes)OverflowError: int too large to convert to float當我嘗試對其進行故障排除時,此錯誤發生在該部分codes = pd.Series(    [int(''.join(row)) for row in columns.itertuples(index=False)],    index=df.index)如果我將最后一個服務子代碼更改為 60 或更低的數字而不是 699,此錯誤就會消失。這個錯誤有什么解決辦法嗎?我希望它甚至可以用于 5 位數字。尋找永久解決方案
查看完整描述

1 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

  • 罪魁禍首似乎是 pandas 試圖將值轉換為浮點數。

    • [int(''.join(row)) for row in columns.itertuples(index=False)]有效,但將其轉換為系列卻pd.Series無效。

    • 我不知道為什么 pandas 試圖將intstofloats

  • 解決方法是,以熊貓沒有機會嘗試將其轉換intsfloats.

  • dfg[0]list一個int

  • 以下代碼也適用于'ServiceSubCodeKey'等于99999

import pandas as pd


# this will create codes

codes_values = [int(''.join(r)) for r in columns.itertuples(index=False)]

codes = pd.Series({'test': codes_values}).explode()

codes.index = df.index


# groupby and aggregate the values into lists

dfg = codes.groupby(df.Id).agg(list).reset_index()


# sum the lists; doing this with a pandas function also does not work, so no .sum or .apply

summed_lists = list()

for r, v in dfg.iterrows():

    summed_lists.append(str(sum(v[0])))


# assign the list of strings to a column

dfg['sums'] = summed_lists


# perform the remainder of the functions on the sums column

dfg['final'] = dfg.sums.str.pad(width=columns.shape[1], fillchar='0').str.rstrip('0')


# display(dfg.final)

0                                                 0101

1                                                   01

2                                            000000001

3                                                   01

4                                              1011001

5    0000000000000000000000000000000000000000000000...

Name: final, dtype: object


# merge df and dfg.final

dfm = pd.merge(df, dfg[['Id', 'final']], on='Id')


# display(dfm)

        Id  ServiceSubCodeKey   PrintDate         final

0  1895650                  2  2018-07-27          0101

1  1895650                  4  2018-08-13          0101

2  1896355                  2  2018-08-10            01

3  1897675                  9  2018-08-13     000000001

4  1897843                  2  2018-08-10            01

5  2178737                  3  2019-06-14       1011001

6  2178737                  4  2019-06-14       1011001

7  2178737                  7  2019-06-14       1011001

8  2178737                  1  2019-06-14       1011001

9  2178750              99999  2019-06-14  ...000000001


查看完整回答
反對 回復 2023-04-11
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號