Python 2.7 将带逗号的字符串列更改为浮点

Python 2.7 将带逗号的字符串列更改为浮点,python-2.7,pandas,dataframe,typeerror,Python 2.7,Pandas,Dataframe,Typeerror,我运行了代码: df["VotesPerYear"] = df["Votes"]/df["Years"] 并收到错误信息: "TypeError: unsupported operand type(s) for /: 'unicode' and 'float'" df[“voates”]是一个数字字符串,逗号作为千位分隔符。将其转换为浮点数以便执行操作的最佳方法是什么?例如,如果df['vots']中的每个元素的形式为u'x,xxx,xxx.xx, 我们可以做到: df["Votes

我运行了代码:

df["VotesPerYear"] = df["Votes"]/df["Years"]
并收到错误信息:

"TypeError: unsupported operand type(s) for /: 'unicode' and 'float'"

df[“voates”]是一个数字字符串,逗号作为千位分隔符。将其转换为浮点数以便执行操作的最佳方法是什么?

例如,如果df['vots']中的每个元素的形式为u'x,xxx,xxx.xx, 我们可以做到:

    df["VotesPerYear"] = df["Votes"].map(lambda x: float(''.join(x.split(',')))) / df["Years"]
您可以使用将
更改为nothing,然后使用以下方法将列转换为float:


您是如何加载这些数据的?如果使用了
read_csv
,则可以告诉pandas将逗号视为千分分隔符:
read_csv(千分=,)
df["VotesPerYear"] = df["Votes"].str.replace(",", "").astype(float) / df["Years"]