How use the Pandas .assign() method chain on a MultiIndex Column? How use the Pandas .assign() method chain on a MultiIndex Column? pandas pandas

How use the Pandas .assign() method chain on a MultiIndex Column?


If I'm reading this correctly, would it not be as simple as:

Original df:

first        barsecond       one       twoA       0.386729  1.014010B       0.236824  0.439019C       0.530020 -0.268751

Code:

df[('bar','one')] *= 10

Updated df (modify column):

first         barsecond        one       twoA       3.8672946  1.014010B       2.3682376  0.439019C       5.3002040 -0.268751

Or, updated df (create new column):

df[('bar','new')] = df[('bar','one')] * 10first        barsecond       one       two       newA       0.386729  1.014010  3.867295B       0.236824  0.439019  2.368238C       0.530020 -0.268751  5.300204


Just to get more info in the same place - here's this issue raised (by you!) on GitHub and the response was:

you can simply directly index

df[('a', 1)] = ...

.assign cannot support this syntax as its a function call, where a tuple is not a valid identifier.


This workaround that uses method chaining would give you the result you want.

df = (df.assign(barOne=lambda x: x.loc[:, ('bar', 'one')]*10)        .rename(columns={'':'barOne'}, level=1)        .rename(columns={'barOne':'bar'}, level=0)     )dffirst        barsecond       one       two     barOneA      -0.016595  0.613149  -0.165947B      -1.108934 -2.662668 -11.089339C       0.022323  1.749033   0.223232df.columnsMultiIndex([('bar',    'one'),            ('bar',    'two'),            ('bar', 'barOne')],           names=['first', 'second'])