获取错误:int()参数必须是字符串、类似字节的对象或数字,而不是'numpy.dtype'

2024-03-28 20:57:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我导入此csv文件:

sales = pd.read_csv('http://samplecsvs.s3.amazonaws.com/SalesJan2009.csv')

我正在尝试制作透视表,但我注意到当我这样做时:

sales.pivot_table(index='Product', columns='Payment_Type', values= 'Price' )

我犯了一个错误

Price column is string dtype

然后我尝试将列转换为float并执行以下操作:

sales.Price.astype('float64')

然后,它显示

Error: could not convert string to float: '13,000'

我意识到Price列中有一个值,其中有逗号,所以不能进行浮点转换。因此,我通过下面的方法删除逗号,并使用iloc检查它是否有效,它是否有效,该值更改为13000:

sales['Price']= sales.Price.str.replace(',', '')

sales.iloc[558, 2]

当我尝试将Price列转换为float64时,我再次遇到这个错误,我不理解:

sales['Price'] = sales.Price.astype('int64')

Error: Getting error: int() argument must be a string, a bytes-like object or a number, not 'numpy.dtype'

有人知道发生了什么吗?这是什么类型的numpy.dtype?它既不是numpy.float64也不是numpy.int64

我如何解决这个问题


Tags: csvnumpystring错误noterrorfloatprice
1条回答
网友
1楼 · 发布于 2024-03-28 20:57:24

在你正确的命令之前

sales['Price'] = sales.Price.astype('int64')

您很可能执行了该命令

sales['Price'] = sales.Price.dtype

Price列中的字符串更改为数据类型

重复所有命令(不使用sales['Price'] = sales.Price.dtype),就可以了

相关问题 更多 >