【问题标题】:How to change categorical (consists both alphabet and numbers) into numerical using map in python? [duplicate]如何在 python 中使用 map 将分类(包括字母和数字)更改为数字? [复制]
【发布时间】:2019-03-10 12:48:40
【问题描述】:

我的数据集中有一列 X,其中包含以下数据

1,
2,
4,
5more,
3,
2

所以当我使用

dataset['X'].map({
"5more" : 5
})

它将所有值转换为NaN。 如何在没有 NaN/Infinity 的情况下将这些数据集转换为数值?

【问题讨论】:

    标签: python python-3.x machine-learning


    【解决方案1】:

    使用 map 将所有不在字典中的键替换为 NaN。来自pandas documentationmap

    当 arg 是字典时,Series 中不在字典中的值(作为键)将转换为 NaN。

    你可以试试replace:

    dataset['X'].replace("5more" : 5})
    

    你也可以添加inplace=True来替换原地:

    dataset['X'].replace("5more" : 5}, inplace=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      相关资源
      最近更新 更多