我导入此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
我如何解决这个问题
在你正确的命令之前
您很可能执行了该命令
将
Price
列中的字符串更改为数据类型重复所有命令(不使用
sales['Price'] = sales.Price.dtype
),就可以了相关问题 更多 >
编程相关推荐