我有一个资产价格数据的 CSV 文件,我正在尝试将其导入以在 Python 中进行分析。但是,我无法将列转换为整数,因此我可以实际使用数据。
我尝试消除 NaN 值,但第一个价格数据点仍然存在问题。我尝试使用 to_numeric,但除非我将错误处理更改为“强制”,否则我无法运行代码。
我还在 Excel 中打开了 CSV 文件本身,并且能够毫无问题地对列求和,所以我认为数据本身没有问题。
import pandas as pd
prices = pd.read_csv("btc_usd_10_19.csv")
prices[["Price"]] = prices[["Price"]].apply(pd.to_numeric)
Specific error:
ValueError: ('Unable to parse string "10,874.9" at position 0', 'occurred at index Price')
这是会计格式,因此在转换为数字之前,我们需要将 ',' 替换为 ''
prices["Price"] = pd.to_numeric(prices["Price"].replace({',':''}, regex=True))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句