gpt4 book ai didi

python - 无法在 pandas 中使用数据框元素的平方

转载 作者:行者123 更新时间:2023-12-01 03:05:18 24 4
gpt4 key购买 nike

我试图在Python中对数据框的元素进行平方,但得到一个奇怪的答案,因为它不涉及负号

这是我的数据框:

         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

最佳答案

这是溢出错误。该列的 dtypeint32,它只能保存 -21474836482147483647 之间的整数(即 >-2**312**31 - 1)。例如,第一个值 225000**2 == 50625000000。看看this section of the docs .

您可以使用更大的 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
>>>

关于python - 无法在 pandas 中使用数据框元素的平方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43490591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com