Haliaetus
Haliaetus

Reputation: 490

How to rename columns of a pandas DataFrame using MultiIndex object as part of a method chain?

I have a pandas DataFrame containing data, and a MultiIndex object containing multiple levels of column names. Is there a way I can rename the column names as part of a chain using that multiindex object?

I know I can rename columns using the "pandas.DataFrame.columns" assignment, but that would require a separate statement, which would break the chain.

There is an analog method for indexes: "pandas.DataFrame.set_index". It does exactly what I want, but for indexes. I would need a method like this for columns instead.

Here is a short demonstration of what I attempted:

import numpy as np
import pandas as pd



In [71]:testdf = pd.DataFrame(np.random.rand(6,4))

In [72]:testdf
Out[72]: 
          0         1         2         3
0  0.557704  0.827760  0.160876  0.107557
1  0.538251  0.817143  0.444935  0.665006
2  0.235694  0.721285  0.318727  0.220839
3  0.765107  0.776936  0.993133  0.127687
4  0.416877  0.121863  0.059885  0.332443
5  0.181796  0.685475  0.677187  0.399419

In [73]:cols = pd.MultiIndex.from_tuples(
                (("A", "1"), ("A", "2"),
                 ("B", "1"), ("B", "1")),
                names=("main", "sub"))
In [74]:cols
Out[74]:MultiIndex(levels=[['A', 'B'], ['1', '2']],
           codes=[[0, 0, 1, 1], [0, 1, 0, 0]],
           names=['main', 'sub'])

In [75]:
(testdf
    .divide(6)
    .rename(cols, axis='columns'))
Out[75]: 
TypeError: 'MultiIndex' object is not callable

I was expecting this:

In [76]:testdf
Out[76]: 
main         A                   B          
sub          1         2         1         1
0     0.099468  0.105746  0.115839  0.139009
1     0.107165  0.024658  0.063040  0.165303
2     0.131601  0.111327  0.040034  0.019957
3     0.128644  0.091933  0.060455  0.128705
4     0.092786  0.063429  0.080659  0.084506
5     0.005722  0.120841  0.072167  0.131921

Upvotes: 0

Views: 155

Answers (1)

user2677285
user2677285

Reputation: 313

Sounds like you want set_index but transposed? Here you go:

testdf.divide(6).T.set_index(cols).T

Upvotes: 1

Related Questions