Jas*_*all 3 typeerror dataframe python-2.7 pandas
我运行了代码:
df["VotesPerYear"] = df["Votes"]/df["Years"]
Run Code Online (Sandbox Code Playgroud)
并收到错误:
"TypeError: unsupported operand type(s) for /: 'unicode' and 'float'"
Run Code Online (Sandbox Code Playgroud)
df["Votes"] 是一串数字,以逗号作为千位分隔符。将其转换为浮点数以便我可以执行操作的最佳方法是什么?
您可以使用str.replace更改,为空,然后使用astype方法将列转换为浮动:
df["VotesPerYear"] = df["Votes"].str.replace(",", "").astype(float) / df["Years"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4940 次 |
| 最近记录: |