【问题标题】:Getting error: int() argument must be a string, a bytes-like object or a number, not 'numpy.dtype出现错误:int() 参数必须是字符串、类似字节的对象或数字,而不是 'numpy.dtype
【发布时间】:2020-09-09 14:47:52
【问题描述】:

我导入这个 csv 文件:

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

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

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

我收到一个错误

价格列是字符串dtype

然后我尝试将列转换为浮动并执行此操作:

sales.Price.astype('float64')

然后,它显示

错误:无法将字符串转换为浮点数:'13,000'

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

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

sales.iloc[558, 2]

当我尝试将 Price 列转换为 float64 时,我再次收到这个我不明白的错误:

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

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

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

我该如何解决这个问题?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    在你正确的命令之前

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

    你很可能执行了命令

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

    Price 列中的字符串更改为 dtypes。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2021-02-08
      • 1970-01-01
      相关资源
      最近更新 更多