【发布时间】:2016-03-31 10:54:24
【问题描述】:
我运行了代码:
df["VotesPerYear"] = df["Votes"]/df["Years"]
并收到错误:
"TypeError: unsupported operand type(s) for /: 'unicode' and 'float'"
df["Votes"] 是一串以逗号作为千位分隔符的数字。将它转换为浮点数以便我可以执行操作的最佳方法是什么?
【问题讨论】:
-
您是如何加载这些数据的?如果您使用了
read_csv,那么您可以告诉 pandas 将逗号视为千位分隔符:read_csv(thousands=',')
标签: python-2.7 pandas dataframe typeerror