[英]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
. 我想增加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)]
但是我真正想要的是将MultiIndex列设为[(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. 如果可能的话,我想一起使用pipe / apply / assign样式的链接方法。
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 您可以使用MultiIndex
再次创建列,然后将其分配回来
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.