【问题标题】:Pandas String Series to int normalisation for TensorPandas 字符串系列到张量的 int 规范化
【发布时间】:2018-07-06 19:16:04
【问题描述】:

我有一个带有重复字符串值的 Pandas::Series 对象,我需要将其规范化为 int 值以输入 TensorFlow。

我已经研究过按照this 将其转换为Category,但它会为每个项目创建一个代码,而不是识别重复项。

例如我希望进行以下转换

['a', 'b', 'c', 'd', 'a', 'a', 'c'] -> [1, 2, 3, 4, 1, 1, 3]

【问题讨论】:

    标签: python pandas tensorflow normalization


    【解决方案1】:

    你需要一点改变factorize:

    print ((pd.factorize(['a', 'b', 'c', 'd', 'a', 'a', 'c'])[0] + 1).tolist())
    [1, 2, 3, 4, 1, 1, 3]
    

    【讨论】:

      【解决方案2】:

      转换为类别后需要添加cat.codes

      pd.Series(['a', 'b', 'c', 'd', 'a', 'a', 'c']).astype('category').cat.codes+1
      Out[1407]: 
      0    1
      1    2
      2    3
      3    4
      4    1
      5    1
      6    3
      dtype: int8
      

      【讨论】:

        猜你喜欢
        • 2020-01-11
        • 2010-10-10
        • 2021-07-11
        • 1970-01-01
        • 2020-03-01
        • 2018-09-29
        • 2020-02-12
        • 2012-04-25
        • 1970-01-01
        相关资源
        最近更新 更多