Reputation: 16508
I have a 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 would like to create a "mesh grid" out of the two levels, I am doing the following for now:
level0 = test2.columns.levels[0]
level1 = test2.columns.levels[1]
columnNames = []
for l in level0:
for j in level1:
columnNames.append(l+'_'+j)
columnNames
['t070199_foo', 't070199_fubar', 't070299_foo', 't070299_fubar', ...]
This is exactly what I want, but it doesn't look clean at all. Taking the two sets, I suppose there is a Python
way of intertwining these two more comfortably. Or, perhaps, a natural way that pandas
provides to create "one level column names" out of "2 level column names".
Upvotes: 1
Views: 76
Reputation: 16508
test2.columns.ravel()
gives me the following tuple: ('t070199', 'foo'), ('t070199', 'fubar')...
.
So, I can do
test2.columns = ['_'.join(x) for x in test2.columns.ravel()]
which gives me exactly what I wanted.
Upvotes: 0
Reputation: 15433
One thing you can do to make it a little nicer is using a list comprehension:
columnNames = [lev0 + '_' + lev1 for lev0 in level0 for lev1 in level1]
Upvotes: 2