Reconstruct a categorical variable from dummies in pandas -
get_dummies allows convert categorical variable dummy variables. besides fact it's trivial reconstruct categorical variable, there preferred/quick way it?
in [46]: s = series(list('aaabbbccddefgh')).astype('category') in [47]: s out[47]: 0 1 2 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]: 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() # don't think need specify of categories here, definition # in dummy matrix start (and hence column index) in [51]: series(pd.categorical(x[x!=0].index.get_level_values(1))) out[51]: 0 1 2 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 think need function 'do' seems natural operations. maybe get_categories()
, see here
pandas
No comments:
Post a Comment