Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我不能将Dataframe列转换为整数?_Python_Pandas - Fatal编程技术网

Python 为什么我不能将Dataframe列转换为整数?

Python 为什么我不能将Dataframe列转换为整数?,python,pandas,Python,Pandas,我有一个资产价格数据的CSV,我正试图将其导入Python进行分析。但是,我无法将列转换为整数,以便实际使用数据 我试图消除NaN值,但第一个价格数据点仍然存在问题。我尝试使用to_numeric,但除非我将错误处理更改为“强制”,否则我无法运行代码 我也在Excel中打开了CSV文件本身,并且能够对列进行求和而没有问题,因此我认为数据本身没有问题 import pandas as pd prices = pd.read_csv("btc_usd_10_19.csv") prices[["P

我有一个资产价格数据的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))

在将列转换为int之前,需要删除Price列中的字符。请尝试prices['Price']=prices['Price']。替换“,”,将数千=”,”添加到pd。read_csvWorked完美!非常感谢。