Python 无法在中使用dataframe元素的平方
我试图在Python中对数据帧的元素进行平方运算,但得到了一个奇怪的答案,因为它不涉及负号 这是我的数据帧:Python 无法在中使用dataframe元素的平方,python,Python,我试图在Python中对数据帧的元素进行平方运算,但得到了一个奇怪的答案,因为它不涉及负号 这是我的数据帧: Risk_Weight weighted_sum_sens 0 45 225000 1 45 -1800000 2 45 450000 3 48 480000 4
Risk_Weight weighted_sum_sens
0 45 225000
1 45 -1800000
2 45 450000
3 48 480000
4 99 495000
5 10 100000
6 10 -100000
7 49 245000
8 47 -235000
In[29]:vec = (ws.weighted_sum_sens**2)
vec
Out[29]:
0 -914607552
1 1594658816
2 636537088
3 -1528233984
4 211864128
5 1410065408
6 1410065408
7 -104542144
8 -609574848
Name: weighted_sum_sens, dtype: int32
这是溢出错误。该列的
dtype
是int32
,它只能保存-2147483648
到2147483647
之间的整数(即-2**31
到2**31-1
)。并且,例如,第一个值225000**2==506500000
。看一看
您可以使用更大的int
类型:
>>> df**2
Risk_Weight weighted_sum_sens
0 2025 -914607552
1 2025 1594658816
2 2025 636537088
3 2304 -1528233984
4 9801 211864128
5 100 1410065408
6 100 1410065408
7 2401 -104542144
8 2209 -609574848
>>> df['weighted_sum_sens'] = df.weighted_sum_sens.astype(np.int64)
>>> df**2
Risk_Weight weighted_sum_sens
0 2025 50625000000
1 2025 3240000000000
2 2025 202500000000
3 2304 230400000000
4 9801 245025000000
5 100 10000000000
6 100 10000000000
7 2401 60025000000
8 2209 55225000000
>>>
编辑您的问题,以便我们知道代码的哪一部分会导致问题,您希望得到什么数据以及您得到什么。请用正确的英语来做。这对我来说很有用,发布原始数据,代码来重新创建你的df,以便其他人复制。还有你的熊猫和裸体版本是什么?
df.info()
显示了什么?