我运行了代码:
df["VotesPerYear"] = df["Votes"]/df["Years"]
并收到错误:
"TypeError: unsupported operand type(s) for /: 'unicode' and 'float'"
df["Votes"] 是一串以逗号作为千位分隔符的数字。将其转换为 float 以便我执行操作的最佳方法是什么?
最佳答案
您可以使用 str.replace
将 ,
更改为空,然后使用 astype
将列转换为 float 方法:
df["VotesPerYear"] = df["Votes"].str.replace(",", "").astype(float) / df["Years"]
关于python-2.7 - 将带有逗号的 Pandas 字符串列更改为 Float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34462207/