Reconstruct a categorical variable from dummies in pandas

themiurgo

pd.get_dummies allows to convert a categorical variable into dummy variables. Besides the fact that it's trivial to reconstruct the categorical variable, is there a preferred/quick way to do it?

Jeff
In [46]: s = Series(list('aaabbbccddefgh')).astype('category')

In [47]: s
Out[47]: 
0     a
1     a
2     a
3     b
4     b
5     b
6     c
7     c
8     d
9     d
10    e
11    f
12    g
13    h
dtype: category
Categories (8, object): [a < b < c < d < e < f < g < h]

In [48]: df = pd.get_dummies(s)

In [49]: df
Out[49]: 
    a  b  c  d  e  f  g  h
0   1  0  0  0  0  0  0  0
1   1  0  0  0  0  0  0  0
2   1  0  0  0  0  0  0  0
3   0  1  0  0  0  0  0  0
4   0  1  0  0  0  0  0  0
5   0  1  0  0  0  0  0  0
6   0  0  1  0  0  0  0  0
7   0  0  1  0  0  0  0  0
8   0  0  0  1  0  0  0  0
9   0  0  0  1  0  0  0  0
10  0  0  0  0  1  0  0  0
11  0  0  0  0  0  1  0  0
12  0  0  0  0  0  0  1  0
13  0  0  0  0  0  0  0  1

In [50]: x = df.stack()

# I don't think you actually need to specify ALL of the categories here, as by definition
# they are in the dummy matrix to start (and hence the column index)
In [51]: Series(pd.Categorical(x[x!=0].index.get_level_values(1)))
Out[51]: 
0     a
1     a
2     a
3     b
4     b
5     b
6     c
7     c
8     d
9     d
10    e
11    f
12    g
13    h
Name: level_1, dtype: category
Categories (8, object): [a < b < c < d < e < f < g < h]

So I think we need a function to 'do' this as it seems to be a natural operations. Maybe get_categories(), see here

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Recoding Categorical Variable (Stata)

来自分类Dev

包含列表的Pandas列的get_dummies

来自分类Dev

嵌套表的Pandas Get_dummies

来自分类Dev

来自Pandas get_dummies的重复列

来自分类Dev

pandas get_dummies 语法错误

来自分类Dev

get_dummies() 用于多个 Pandas DataFrame

来自分类Dev

Aggregating Dataset to "ignore" categorical variable

来自分类Dev

从pandas.df_dummies回来的最优雅的方式

来自分类Dev

如何使pandas.Series.str.get_dummies()报告NaN?

来自分类Dev

如何在 Python 中存储 Pandas 的 get_dummies 转换?

来自分类Dev

在sklearn中使用Pandas'categorical'dtype

来自分类Dev

如何通过pandas get_dummies()方法为某些列创建虚拟对象?

来自分类常见问题

get_dummies(Pandas)和OneHotEncoder(Scikit-learn)之间的优缺点是什么?

来自分类Dev

将`pandas.get_dummies`转换为新数据的简单方法?

来自分类Dev

one-hot编码器实现pandas.get_dummies,如何读取语法

来自分类Dev

如何解决 pandas.get_dummies 异常:数据必须是一维的

来自分类Dev

R - ggplot2 re-order of categorical variable (issue with reorder func)

来自分类Dev

One Hot Encoding和pandas.categorical.code有什么区别

来自分类Dev

Keyboard hook for dummies

来自分类Dev

反向get_dummies()

来自分类Dev

Python Pandas:将2,000,000个DataFrame行转换为二进制矩阵(pd.get_dummies()),而不会发生内存错误?

来自分类Dev

get_dummies分割字符

来自分类Dev

Bash: Update the parent variable from function variable

来自分类Dev

python pandas .map double variable lambda

来自分类Dev

Access class variable from instance

来自分类Dev

Mutable variable is accessible from closure

来自分类Dev

Remove a substring from a bash variable

来自分类Dev

在多个DataFrame列上运行get_dummies?

来自分类Dev

熊猫:与get_dummies的共现矩阵

Related 相关文章

  1. 1

    Recoding Categorical Variable (Stata)

  2. 2

    包含列表的Pandas列的get_dummies

  3. 3

    嵌套表的Pandas Get_dummies

  4. 4

    来自Pandas get_dummies的重复列

  5. 5

    pandas get_dummies 语法错误

  6. 6

    get_dummies() 用于多个 Pandas DataFrame

  7. 7

    Aggregating Dataset to "ignore" categorical variable

  8. 8

    从pandas.df_dummies回来的最优雅的方式

  9. 9

    如何使pandas.Series.str.get_dummies()报告NaN?

  10. 10

    如何在 Python 中存储 Pandas 的 get_dummies 转换?

  11. 11

    在sklearn中使用Pandas'categorical'dtype

  12. 12

    如何通过pandas get_dummies()方法为某些列创建虚拟对象?

  13. 13

    get_dummies(Pandas)和OneHotEncoder(Scikit-learn)之间的优缺点是什么?

  14. 14

    将`pandas.get_dummies`转换为新数据的简单方法?

  15. 15

    one-hot编码器实现pandas.get_dummies,如何读取语法

  16. 16

    如何解决 pandas.get_dummies 异常:数据必须是一维的

  17. 17

    R - ggplot2 re-order of categorical variable (issue with reorder func)

  18. 18

    One Hot Encoding和pandas.categorical.code有什么区别

  19. 19

    Keyboard hook for dummies

  20. 20

    反向get_dummies()

  21. 21

    Python Pandas:将2,000,000个DataFrame行转换为二进制矩阵(pd.get_dummies()),而不会发生内存错误?

  22. 22

    get_dummies分割字符

  23. 23

    Bash: Update the parent variable from function variable

  24. 24

    python pandas .map double variable lambda

  25. 25

    Access class variable from instance

  26. 26

    Mutable variable is accessible from closure

  27. 27

    Remove a substring from a bash variable

  28. 28

    在多个DataFrame列上运行get_dummies?

  29. 29

    熊猫:与get_dummies的共现矩阵

热门标签

归档