【问题标题】:How to get a pandas Series dummy representation of one value如何获得一个值的熊猫系列虚拟表示
【发布时间】:2019-04-11 01:24:48
【问题描述】:

假设我有 5 个不同的类别

categories = {'a', 'b', 'c', 'd', 'e'}

是否可以使用 Pandas 的get_dummies 来获得一个元素的虚拟表示?即,假设我有

element = 'a'

变成

Series({
 'a' : 1, 
 'b' : 0,
 'c' : 0,
 'd' : 0,
 'e' : 0,
 })

【问题讨论】:

    标签: python pandas machine-learning categorical-data dummy-variable


    【解决方案1】:

    请检查这是否有帮助。这将生成所有类别的假人。

    categories = {'a', 'b', 'c', 'd', 'e'}
    
    categoriesSeries = pd.Series(list(categories))
    pd.get_dummies(categoriesSeries)
    

    结果

       a  b  c  d  e
    0  0  0  0  0  1
    1  0  0  1  0  0
    2  0  0  0  1  0
    3  0  1  0  0  0
    4  1  0  0  0  0
    

    现在要为一个元素生成一个虚拟对象,您可以像这样传递索引。

    pd.get_dummies(categoriesSeries[4])
    

    结果

       a
    0  1
    

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 1970-01-01
      • 2019-08-17
      • 2020-08-07
      • 1970-01-01
      • 2022-10-07
      • 2018-07-20
      • 2022-01-24
      • 2023-03-25
      相关资源
      最近更新 更多