Skip to main content
 首页 » 编程设计

python之通过对两列中的一些进行分组并保持其他不变来进行 Pandas 多索引

2024年12月31日7artech

我有一个包含一些列的 pandas 数据框,我喜欢将一些列组合在一起,比如组 x 和 y 表示均值,x 和 y 方差根据 varinaces 等等。

问题设置如下:

data = """ 
xbar    ybar    sx2 sy2 sp2 margin_err  ci_low  ci_high 
0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.00    0.00 
0.750000    0.083333    0.750000    0.083333    0.416667    0.452507    0.21    1.12 
0.166667    0.166667    0.333333    0.333333    0.333333    0.404735    -0.40   0.40 
0.083333    0.500000    0.083333    0.636364    0.359848    0.420524    -0.84   0.00 
0.166667    0.583333    0.333333    0.992424    0.662879    0.570753    -0.99   0.15 
1.833333    1.250000    1.606061    0.931818    1.268939    0.789681    -0.21   1.37 
1.250000    1.583333    2.022727    2.265152    2.143939    1.026449    -1.36   0.69 
3.250000    5.333333    4.931818    16.060606   10.496212   2.271159    -4.35   0.19 
6.083333    7.333333    12.265152   12.242424   12.253788   2.453954    -3.70   1.20 
8.083333    6.666667    7.719697    4.969697    6.344697    1.765780    -0.35   3.18 
3.166667    3.250000    5.060606    6.568182    5.814394    1.690376    -1.77   1.61 
2.500000    1.916667    1.545455    3.719697    2.632576    1.137422    -0.55   1.72 
0.833333    0.750000    0.696970    1.113636    0.905303    0.667004    -0.58   0.75 
1.250000    0.500000    1.659091    0.636364    1.147727    0.751018    -0.00   1.50 
0.166667    0.500000    0.333333    2.090909    1.212121    0.771799    -1.11   0.44 
"""; 
 
import numpy as np 
import pandas as pd 
import io 
 
df = pd.read_csv(io.StringIO(data.lstrip().strip()),sep=r'\s+') 
df 

期望的输出是这样的: https://imgur.com/a/mJbwSM6

期望的输出

Mean            Variance Pooled_variance Margin_of_error 90%_CI 
xbar    ybar    sx2 sy2 sp2 margin_err  ci_low  ci_high 
0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.00    0.00 
0.750000    0.083333    0.750000    0.083333    0.416667    0.452507    0.21    1.12 

我的尝试:

df.columns = ['Mean_GER1','Mean_GER2','Var_GER1','Var_GER2','Var_pooled','Margin_of_error','CI_90_low','CI_90_high'] 
df 

如何获取此数据框的多索引列?

请您参考如下方法:

我查看了 Pandas 多索引教程并提出了这个解决方案:

mi = pd.MultiIndex.from_tuples([('Mean', 'x'), ('Mean', 'y'), ('Sample_Variance', 'x'), ('Sample_Variance', 'y'),('Pooled_Var',''), ('Margin_of_Err',''),('95% CI','low'),('95% CI','high')]) 
 
pd.DataFrame(df.values, columns=mi) 

结果:

         Mean                 Sample_Variance      Pooled_Var Margin_of_Err 95% CI 
    x           y               x          y                                low   high 
0   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.00    0.00 
1   0.750000    0.083333    0.750000    0.083333    0.416667    0.452507    0.21    1.12 
2   0.166667    0.166667    0.333333    0.333333    0.333333    0.404735    -0.40   0.40 
3   0.083333    0.500000    0.083333    0.636364    0.359848    0.420524    -0.84   0.00 
4   0.166667    0.583333    0.333333    0.992424    0.662879    0.570753    -0.99   0.15 
5   1.833333    1.250000    1.606061    0.931818    1.268939    0.789681    -0.21   1.37 
6   1.250000    1.583333    2.022727    2.265152    2.143939    1.026449    -1.36   0.69 
7   3.250000    5.333333    4.931818    16.060606   10.496212   2.271159    -4.35   0.19 
8   6.083333    7.333333    12.265152   12.242424   12.253788   2.453954    -3.70   1.20 
9   8.083333    6.666667    7.719697    4.969697    6.344697    1.765780    -0.35   3.18 
10  3.166667    3.250000    5.060606    6.568182    5.814394    1.690376    -1.77   1.61 
11  2.500000    1.916667    1.545455    3.719697    2.632576    1.137422    -0.55   1.72 
12  0.833333    0.750000    0.696970    1.113636    0.905303    0.667004    -0.58   0.75 
13  1.250000    0.500000    1.659091    0.636364    1.147727    0.751018    -0.00   1.50 
14  0.166667    0.500000    0.333333    2.090909    1.212121    0.771799    -1.11   0.44