对数据帧列求和会产生意外输出



在我的数据集中,有两列我想求和,以找到它们在计算和中的比例。为此我做了这个:

complaints_by_state['total_complaints'] = complaints_by_state.Open + complaints_by_state.Closed

除了某一行外,它运行良好。如果你看看乔治亚州,208和80的总和被计算为32:

Closed   Open    percentage_unresolved   total_complaints
State               
Alabama  17  9   0.346154   26
Arizona  14  6   0.300000   20
Arkansas    6   0   0.000000    6
California  159 61  0.277273    220
Colorado    58  22  0.275000    80
Connecticut 9   3   0.250000    12
Delaware    8   4   0.333333    12
Columbia    14  2   0.125000    16
Florida 201 39  0.162500    240
Georgia 208 80  2.500000    32
Illinois    135 29  0.176829    164

这里发生了什么,如何解决?

您似乎在使用8位整数

import numpy as np
np.uint8(208) + np.uint8(80)
# returns
<stdin>:1: RuntimeWarning: overflow encountered in ubyte_scalars
32

使用complaints_by_state.dtypes进行检查。如果使用

complaints_by_state['Open'] = complaints_by_state['Open'].astype(np.int)

等等。

最好在导入数据时修复此问题。

最新更新