I have a MultiIndexed DataFrame with codes for countries as follows:
In [3]: idx = pd.MultiIndex.from_tuples([('AUS', 'a'), ('AUS', 'b'), ('BRA', 'a')])
In [4]: idx.names = ['country', 'foo']
In [5]: df = pd.DataFrame([4,5,6], index=idx)
In [6]: df
Out[6]:
0
country foo
AUS a 4
b 5
BRA a 6
I also have a dictionary with values to replace my codes with:
In [7]: codes = dict(AUS='Australia', BRA='Brazil')
I'd like to do the equivalent of df.replace(codes) but on the index levels (either all levels, or a specific one, I don't mind)
The output would look like:
0
country foo
Australia a 4
b 5
Brazil a 6
I'm currently doing it in a very silly way indeed:
In [21]: replaced = [pd.Series(df.index.get_level_values(i)).replace(codes) for i in range(len(df.index.levels))]
In [22]: replaced_tuples = zip(*replaced)
In [23]: new_idx = pd.MultiIndex.from_tuples(replaced_tuples)
In [27]: df_replaced = pd.DataFrame(df.values, index=new_idx)
In [28]: df_replaced
Out[28]:
0
Australia a 4
b 5
Brazil a 6
What's the much nicer way that's staring me in the face? (Note that this method doesn't even preserve level names so it's all-round bad.)