【问题标题】:cut function : Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'剪切函数:无法根据规则“安全”将数组数据从 dtype('float64') 转换为 dtype('<U32')
【发布时间】:2021-01-01 11:49:04
【问题描述】:

我想将我的 Dataframe 中的列中的内容更改为“好”或“坏”。 该列填充有从 1 到 10 的数字。 1-5是坏的,6-10是好的。 为此,我想使用 cut 方法。

bins = (1, 5.5, 10)
rating = ['bad', 'good']
game['useropinion'] = pd.cut(rating, bins)

运行后的结果:

Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'

怎么了?我该如何解决?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    你可以这样做:

    game['useropinion'] = pd.cut(game['useropinion'], bins, labels=rating)
    

    编辑: 为了回答问题所在,您正在尝试削减评级而不是用户意见数据,因此很自然地会得到一个 TypeError,因为评级是一个字符串数组,而您的 bin 是数字的。 '

    【讨论】:

    • 所以,这是因为打字错误?
    • 这是由于无意中使用了该功能造成的。
    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 2019-02-26
    • 2018-08-15
    • 1970-01-01
    • 2021-01-26
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多