Python 将数据帧中的字符串转换为浮点

Python 将数据帧中的字符串转换为浮点,python,pandas,dataframe,dtype,Python,Pandas,Dataframe,Dtype,我有以下数据框: Bestand = pd.DataFrame({'ISIN': ['NaN', 'IE00B4X9L533', 'IE00BF4RFH31'], 'Marktwert': ['217.803,37', '47.755,10', '15.353,32']}) 为了使用第二列“Marktwert”进行计算,我必须将字符串转换为浮点,sting采用德语格式,这意味着小数点是逗号,千位分隔符是点。编号217.803,37具有数据类型对象 如果我尝试使用

我有以下数据框:

Bestand = pd.DataFrame({'ISIN': ['NaN', 'IE00B4X9L533', 'IE00BF4RFH31'],
                'Marktwert': ['217.803,37', '47.755,10', '15.353,32']})
为了使用第二列“Marktwert”进行计算,我必须将字符串转换为浮点,sting采用德语格式,这意味着小数点是逗号,千位分隔符是点。编号217.803,37具有数据类型对象

如果我尝试使用代码转换

Bestand['Marktwert'] = pd.to_numeric(Bestand['Marktwert'], downcast="float")
我得到了错误

ValueError: Unable to parse string "217.803,37" at position 0

有人能帮我吗?

如果你正在从文件中读取数据,很多Panda的文件读取器都像
Pandas.read_csv()
decimal
数千个
参数来支持这种情况。

Bestand.Marktwert.str.replace('.','.'.'.str.replace('.','.','.')。astype(float)
ISIN和Marktwert的长度不相同。尽量使长度与长度一致same@Cyttorak泰的作品完美!泰,我确实使用read_csv(),但我不熟悉它,我可以在这里使用什么片段?这是我当前使用的代码:Bestand='Verm�gensaufstellung.csv'Bestand=pd.read_csv(Bestand,sep='')根据:
Bestand=pd.read_csv(Bestand,sep='','千=','千=','十进制=',')
Ty,它还会处理千分位吗?这就是
千分位='.
正在做的。