【发布时间】:2020-09-01 15:22:16
【问题描述】:
我需要将一列的每个值除以 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"].astype(float).div(25) -
pd.to_numeric是将列转换为数字 dtype 的灵活方法
标签: python pandas dataframe divide