难以对值执行数学运算-Python

时间:2020-07-01 01:52:27

标签: python pandas

标题很不言自明,我正在努力对python中的值执行数学运算。 因此,我基本上是从Yahoo Finance获得数据并试图对其进行操作

A = df['Total Assets']  #Isolates Assets
print(A)                #Prints Assets
B = A[3]                #Finds third element of that
print(B)                #Prints it
Output = int(float(B))
Output*3

Heres the error code

因此,在此示例中,我简单地拥有“ 1,376,402”,我想将其乘以3。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

问题是您试图转换为整数的输入中有逗号。

您应该改用以下方法:int(B.replace(",", ""))

这将逗号替换为空字符串,这将从字符串中删除逗号,然后将数字转换为整数。另外,在将值转换为整数之前,无需将其转换为浮点型,因此我在答案中省略了该部分。

我希望这会有所帮助!