python - Renaming stacked columns -
i have column set as
>>> test2.columns multiindex(levels=[[u't070199', u't070299', u't070201', u't070105', u't070104', u'employment'], [u'foo', u'fubar']], labels=[[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]], names=[u'foo', u'status'])
i create "mesh grid" out of 2 levels, doing following now:
level0 = test2.columns.levels[0] level1 = test2.columns.levels[1] columnnames = [] l in level0: j in level1: columnnames.append(l+'_'+j) columnnames ['t070199_foo', 't070199_fubar', 't070299_foo', 't070299_fubar', ...]
this want, doesn't clean @ all. taking 2 sets, suppose there python
way of intertwining these 2 more comfortably. or, perhaps, natural way pandas
provides create "one level column names" out of "2 level column names".
one thing can make little nicer using list comprehension:
columnnames = [lev0 + '_' + lev1 lev0 in level0 lev1 in level1]
Comments
Post a Comment