【问题标题】:Why do i receive an error where integer too large to convert to float in my python script?为什么我会收到一个错误,整数太大而无法在我的 python 脚本中转换为浮点数?
【发布时间】:2020-11-18 12:36:04
【问题描述】:

我是 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-14

columns = (
    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 位数字。寻找永久解决方案

【问题讨论】:

标签: python pandas dataframe integer series


【解决方案1】:
  • 罪魁祸首似乎是 pandas 试图将值转换为浮点数。
    • [int(''.join(row)) for row in columns.itertuples(index=False)] 有效,但将其转换为带有 pd.Series 的系列则无效。
    • 我不知道为什么 pandas 试图将 ints 转换为 floats
  • 解决方法是,以这样一种方式处理数字,使 pandas 没有机会尝试将 ints 转换为 floats
  • dfg[0]listint
  • 以下代码也适用于等于99999'ServiceSubCodeKey'
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多