将 pandas df 列中的值除以数字

divide the values from pandas df column by number

我需要将一列的每个值除以 25。下面是我的代码:

df['Amount'] = df['Amount'].div(25)

我收到一个错误:TypeError: unsupported operand type(s) for /: 'str' and 'int' 然后我尝试使用以下代码将 Object 数据类型转换为 int:

df["Amount"] = df["Amount"].astype(str).astype(int)

错误信息:ValueError: invalid literal for int() with base 10: '0.0'

你可以试试这个:

df["Amount"] = df["Amount"]/25
print(df)

您可以尝试 pd.to_numeric 函数,如果您想指定它是一个浮点列,您可以尝试 downcast 参数,但如果不添加它很可能没问题。

df["Amount"] = pd.to_numeric(df["Amount"], downcast='float')