简体   繁体   中英

Assign columns in pandas multiindex

I have this dataframe:

                mu_post          
z                     c         t
index a   b                      
0     0.0 0.0  0.042824  0.051212
      0.5 0.5  0.048293  0.058130
      1.0 1.0  0.047267  0.074043
1     0.0 0.0  0.058205  0.054106
      0.5 0.5  0.064153  0.063573
      1.0 1.0  0.056918  0.059572
2     0.0 0.0  0.059032  0.052211
      0.5 0.5  0.070616  0.066792
      1.0 1.0  0.056892  0.045061

produced by:

import pandas as pd
df = pd.DataFrame({('mu_post', 'c'): {(0, 0.0, 0.0): 0.042824223871028126, (0, 0.5, 0.5): 0.04829260822563669, (0, 1.0, 1.0): 0.047267365970316805, (1, 0.0, 0.0): 0.05820509767743391, (1, 0.5, 0.5): 0.06415323721481726, (1, 1.0, 1.0): 0.0569177959009184, (2, 0.0, 0.0): 0.05903204294019807, (2, 0.5, 0.5): 0.07061613725719014, (2, 1.0, 1.0): 0.056892088025082874}, ('mu_post', 't'): {(0, 0.0, 0.0): 0.051212446939110846, (0, 0.5, 0.5): 0.058129980845875964, (0, 1.0, 1.0): 0.07404310411549644, (1, 0.0, 0.0): 0.05410577324029455, (1, 0.5, 0.5): 0.06357338131851693, (1, 1.0, 1.0): 0.0595723832219094, (2, 0.0, 0.0): 0.05221119083827467, (2, 0.5, 0.5): 0.06679207329135116, (2, 1.0, 1.0): 0.04506069626935631}})

I want to add odds .

def odds(p):
    return p / (1-p)

I can assign like this:

df.assign(
        odds_c=lambda x: odds(x[('mu_post', 'c')]),
        odds_t=lambda x: odds(x[('mu_post', 't')]),
)

             mu_post              odds_c    odds_t
                   c         t                    
 0 0.0 0.0  0.042824  0.051212  0.044740  0.053977
   0.5 0.5  0.048293  0.058130  0.050743  0.061718
   1.0 1.0  0.047267  0.074043  0.049612  0.079964
 1 0.0 0.0  0.058205  0.054106  0.061802  0.057201
   0.5 0.5  0.064153  0.063573  0.068551  0.067889
   1.0 1.0  0.056918  0.059572  0.060353  0.063346
 2 0.0 0.0  0.059032  0.052211  0.062735  0.055087
   0.5 0.5  0.070616  0.066792  0.075982  0.071573
   1.0 1.0  0.056892  0.045061  0.060324  0.047187

But what I really want is for the columns MultiIndex to be [(mu_post, c), (mu_post, t), (odds, c), (odds, t)]

If possible I'd like to use the pipe/apply/assign style of chaining methods together.

You could do join/apply/rename:

In [188]: df.join(df.apply(odds).rename(columns={"mu_post": "odds"}))
Out[188]: 
            mu_post                odds          
                  c         t         c         t
0 0.0 0.0  0.042824  0.051212  0.044740  0.053977
  0.5 0.5  0.048293  0.058130  0.050743  0.061718
  1.0 1.0  0.047267  0.074043  0.049612  0.079964
1 0.0 0.0  0.058205  0.054106  0.061802  0.057201
  0.5 0.5  0.064153  0.063573  0.068551  0.067889
  1.0 1.0  0.056918  0.059572  0.060353  0.063346
2 0.0 0.0  0.059032  0.052211  0.062735  0.055087
  0.5 0.5  0.070616  0.066792  0.075982  0.071573
  1.0 1.0  0.056892  0.045061  0.060324  0.047187

You can create the columns again by MultiIndex using then assign it back

dd=df.assign(
        odds_c=lambda x: odds(x[('mu_post', 'c')]),
        odds_t=lambda x: odds(x[('mu_post', 't')]),
)
dd.columns=pd.MultiIndex.from_product([['mu_post','odd'],['c','t']])
dd
Out[506]: 
            mu_post                 odd          
                  c         t         c         t
0 0.0 0.0  0.042824  0.051212  0.044740  0.053977
  0.5 0.5  0.048293  0.058130  0.050743  0.061718
  1.0 1.0  0.047267  0.074043  0.049612  0.079964
1 0.0 0.0  0.058205  0.054106  0.061802  0.057201
  0.5 0.5  0.064153  0.063573  0.068551  0.067889
  1.0 1.0  0.056918  0.059572  0.060353  0.063346
2 0.0 0.0  0.059032  0.052211  0.062735  0.055087
  0.5 0.5  0.070616  0.066792  0.075982  0.071573
  1.0 1.0  0.056892  0.045061  0.060324  0.047187

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM