合并两个数据帧并使用multiindex创建一个新数据帧

时间:2012-12-07 11:23:52

标签: python dataframe pandas multi-index

我都是,

我在Pandas中有两个数据帧:

一个

In [96]: a
Out[96]: 
      count  mean   std  min    max   25%   50%  75%
10m  604656  4.19  2.43    0  25.92  2.43  3.71  5.5

In [98]: a.to_dict()
Out[98]: 
{'25%': {'10m': 2.429999828338623},
 '50%': {'10m': 3.7100000381469727},
 '75%': {'10m': 5.5},
 'count': {'10m': 604656.0},
 'max': {'10m': 25.920000076293945},
 'mean': {'10m': 4.1893915969076261},
 'min': {'10m': 0.0},
 'std': {'10m': 2.4321994530033586}}

b

In [97]: b
Out[97]: 
              count  mean   std  min    max   25%   50%   75%
0.0_90.0     119842  3.34  1.72    0  14.37  2.08  3.06  4.37
180.0_270.0  234074  5.11  2.82    0  25.92  2.99  4.64  6.82
270.0_360.0  126376  3.79  2.19    0  19.55  2.12  3.40  5.13
90.0_180.0   124364  3.67  1.83    0  14.93  2.37  3.40  4.70

In [99]: b.to_dict()
Out[99]: 
{'25%': {'0.0_90.0': 2.0799999237060547,
  '180.0_270.0': 2.9900000095367432,
  '270.0_360.0': 2.119999885559082,
  '90.0_180.0': 2.3681280016899109},
 '50%': {'0.0_90.0': 3.0579087734222412,
  '180.0_270.0': 4.6399998664855957,
  '270.0_360.0': 3.4000000953674316,
  '90.0_180.0': 3.4006340503692627},
 '75%': {'0.0_90.0': 4.369999885559082,
  '180.0_270.0': 6.8199996948242188,
  '270.0_360.0': 5.130000114440918,
  '90.0_180.0': 4.6960808038711548},
 'count': {'0.0_90.0': 119842.0,
  '180.0_270.0': 234074.0,
  '270.0_360.0': 126376.0,
  '90.0_180.0': 124364.0},
 'max': {'0.0_90.0': 14.369999885559082,
  '180.0_270.0': 25.920000076293945,
  '270.0_360.0': 19.549999237060547,
  '90.0_180.0': 14.930000305175781},
 'mean': {'0.0_90.0': 3.3417930869221379,
  '180.0_270.0': 5.1125810579269269,
  '270.0_360.0': 3.7938859684522601,
  '90.0_180.0': 3.670476718299061},
 'min': {'0.0_90.0': 0.0,
  '180.0_270.0': 0.0,
  '270.0_360.0': 0.0,
  '90.0_180.0': 0.0},
 'std': {'0.0_90.0': 1.7153268584149644,
  '180.0_270.0': 2.8194581011555386,
  '270.0_360.0': 2.1909571297061241,
  '90.0_180.0': 1.8334834361369423}}

我想将新数据框中的两个数据框合并为多索引,如:

new_df

                   count  mean   std  min    max   25%   50%   75%
10m   all          604656  4.19  2.43    0  25.92  2.43  3.71  5.5
      0.0_90.0     119842  3.34  1.72    0  14.37  2.08  3.06  4.37
      180.0_270.0  234074  5.11  2.82    0  25.92  2.99  4.64  6.82
      270.0_360.0  126376  3.79  2.19    0  19.55  2.12  3.40  5.13
      90.0_180.0   124364  3.67  1.83    0  14.93  2.37  3.40  4.70

具有多索引('10m','all')的元素是 a ,接下来的行是 b

有人知道如何在熊猫中实现这个目标吗?

非常感谢,

格雷格

修改

大家好,

我向前移动并向高处扩展。我现在有一个问题,因为高度和扇区不是从低到高的高度,从低扇区到高扇区。

这就是我得到的:

In [141]: df_stats_windSpeed
Out[142]:
                        count  mean   std  min    max   25%   50%    75%
Height Sector                                                           
10m    All             604656  4.19  2.43    0  25.92  2.43  3.71   5.50
       [0.0, 90.0[     119842  3.34  1.72    0  14.37  2.08  3.06   4.37
       [180.0, 270.0[  234074  5.11  2.82    0  25.92  2.99  4.64   6.82
       [270.0, 360.0]  126376  3.79  2.19    0  19.55  2.12  3.40   5.13
       [90.0, 180.0[   124364  3.67  1.83    0  14.93  2.37  3.40   4.70
140m   All             604656  7.85  3.63    0  35.20  5.19  7.70  10.20
       [0.0, 90.0[     116374  6.69  2.89    0  22.86  4.49  6.67   8.80
       [180.0, 270.0[  243590  8.95  3.83    0  35.20  6.29  8.86  11.37
       [270.0, 360.0]  135292  7.22  3.40    0  29.81  4.84  6.98   9.23
       [90.0, 180.0[   109400  7.39  3.46    0  20.91  4.62  7.31  10.10
200m   All             604656  8.47  4.08    0  34.88  5.38  8.21  11.20
       [0.0, 90.0[     113475  7.07  3.25    0  24.56  4.57  6.92   9.45
       [180.0, 270.0[  242157  9.80  4.30    0  34.88  6.65  9.74  12.71
       [270.0, 360.0]  143254  7.75  3.74    0  33.73  5.08  7.48  10.00
       [90.0, 180.0[   105770  7.93  3.96    0  21.54  4.75  7.61  10.86
20m    All             604656  4.82  2.60    0  27.69  2.99  4.33   6.18
       [0.0, 90.0[     116748  3.91  1.81    0  15.59  2.64  3.65   4.95
       [180.0, 270.0[  235304  5.83  2.99    0  27.69  3.67  5.32   7.61
       [270.0, 360.0]  126961  4.35  2.34    0  21.65  2.61  3.93   5.71
       [90.0, 180.0[   125643  4.22  1.96    0  15.98  2.86  3.98   5.29
40m    All             604656  5.68  2.77    0  29.39  3.81  5.29   7.10
       [0.0, 90.0[     120426  4.80  1.99    0  17.69  3.46  4.69   5.97
       [180.0, 270.0[  238381  6.65  3.13    0  29.39  4.48  6.16   8.45
       [270.0, 360.0]  128104  5.36  2.63    0  25.19  3.55  4.98   6.81
       [90.0, 180.0[   117745  4.96  2.11    0  16.79  3.49  4.90   6.22
80m    All             604656  6.84  3.12    0  32.28  4.69  6.66   8.65
       [0.0, 90.0[     119330  5.91  2.44    0  20.74  4.16  5.95   7.58
       [180.0, 270.0[  239146  7.80  3.38    0  32.28  5.54  7.54   9.74
       [270.0, 360.0]  133220  6.42  3.02    0  27.94  4.37  6.15   8.11
       [90.0, 180.0[   112960  6.29  2.71    0  19.53  4.24  6.38   8.28

我想对多索引进行排序,以便高度顺序为:10,20,40,80,140和200m;和行业:'全','[0.0,90.0 [','[90.0,180.0 [','[180.0,270.0 [','[270.0,360.0]'。 我尝试像这样重新索引,但它不起作用

In [255]: df_stats.reindex(index=['10m','20m','40m','80m','200m','140m'],level=0)
In [256]: df_stats.reindex(index=['All','[0.0, 90.0[','[90.0, 180.0[','[180.0, 270.0[','[270.0, 360.0]'],level=1)

这是df dict:

In [257]: df_stats_windSpeed.to_dict()
Out[257]: 
{'25%': {('10m', 'All'): 2.429999828338623,
  ('10m', '[0.0, 90.0['): 2.0799999237060547,
  ('10m', '[180.0, 270.0['): 2.9900000095367432,
  ('10m', '[270.0, 360.0]'): 2.119999885559082,
  ('10m', '[90.0, 180.0['): 2.3681280016899109,
  ('140m', 'All'): 5.1884875297546387,
  ('140m', '[0.0, 90.0['): 4.4935483932495117,
  ('140m', '[180.0, 270.0['): 6.2855626344680786,
  ('140m', '[270.0, 360.0]'): 4.8426017761230469,
  ('140m', '[90.0, 180.0['): 4.6205065250396729,
  ('200m', 'All'): 5.3844937086105347,
  ('200m', '[0.0, 90.0['): 4.572603702545166,
  ('200m', '[180.0, 270.0['): 6.6515130996704102,
  ('200m', '[270.0, 360.0]'): 5.0821070671081543,
  ('200m', '[90.0, 180.0['): 4.749258279800415,
  ('20m', 'All'): 2.9900000095367432,
  ('20m', '[0.0, 90.0['): 2.6400001049041748,
  ('20m', '[180.0, 270.0['): 3.6700000762939453,
  ('20m', '[270.0, 360.0]'): 2.6099998950958252,
  ('20m', '[90.0, 180.0['): 2.8554879426956177,
  ('40m', 'All'): 3.8135370016098022,
  ('40m', '[0.0, 90.0['): 3.4552559852600098,
  ('40m', '[180.0, 270.0['): 4.4779624938964844,
  ('40m', '[270.0, 360.0]'): 3.5464469790458679,
  ('40m', '[90.0, 180.0['): 3.4928045272827148,
  ('80m', 'All'): 4.6858876943588257,
  ('80m', '[0.0, 90.0['): 4.1649158000946045,
  ('80m', '[180.0, 270.0['): 5.5375603437423706,
  ('80m', '[270.0, 360.0]'): 4.3738168478012085,
  ('80m', '[90.0, 180.0['): 4.2378913164138794},
 '50%': {('10m', 'All'): 3.7100000381469727,
  ('10m', '[0.0, 90.0['): 3.0579087734222412,
  ('10m', '[180.0, 270.0['): 4.6399998664855957,
  ('10m', '[270.0, 360.0]'): 3.4000000953674316,
  ('10m', '[90.0, 180.0['): 3.4006340503692627,
  ('140m', 'All'): 7.701094388961792,
  ('140m', '[0.0, 90.0['): 6.6736810207366943,
  ('140m', '[180.0, 270.0['): 8.8593416213989258,
  ('140m', '[270.0, 360.0]'): 6.9792094230651855,
  ('140m', '[90.0, 180.0['): 7.3094825744628906,
  ('200m', 'All'): 8.2149920463562012,
  ('200m', '[0.0, 90.0['): 6.9200782775878906,
  ('200m', '[180.0, 270.0['): 9.7363834381103516,
  ('200m', '[270.0, 360.0]'): 7.4800474643707275,
  ('200m', '[90.0, 180.0['): 7.6083860397338867,
  ('20m', 'All'): 4.3299999237060547,
  ('20m', '[0.0, 90.0['): 3.6500000953674316,
  ('20m', '[180.0, 270.0['): 5.3199996948242187,
  ('20m', '[270.0, 360.0]'): 3.929999828338623,
  ('20m', '[90.0, 180.0['): 3.9796528816223145,
  ('40m', 'All'): 5.291872501373291,
  ('40m', '[0.0, 90.0['): 4.692425012588501,
  ('40m', '[180.0, 270.0['): 6.1558408737182617,
  ('40m', '[270.0, 360.0]'): 4.9811406135559082,
  ('40m', '[90.0, 180.0['): 4.8983759880065918,
  ('80m', 'All'): 6.6626186370849609,
  ('80m', '[0.0, 90.0['): 5.9457294940948486,
  ('80m', '[180.0, 270.0['): 7.544825553894043,
  ('80m', '[270.0, 360.0]'): 6.1506271362304687,
  ('80m', '[90.0, 180.0['): 6.3810868263244629},
 '75%': {('10m', 'All'): 5.5,
  ('10m', '[0.0, 90.0['): 4.369999885559082,
  ('10m', '[180.0, 270.0['): 6.8199996948242188,
  ('10m', '[270.0, 360.0]'): 5.130000114440918,
  ('10m', '[90.0, 180.0['): 4.6960808038711548,
  ('140m', 'All'): 10.203519582748413,
  ('140m', '[0.0, 90.0['): 8.7971394062042236,
  ('140m', '[180.0, 270.0['): 11.370761156082153,
  ('140m', '[270.0, 360.0]'): 9.2274019718170166,
  ('140m', '[90.0, 180.0['): 10.097956657409668,
  ('200m', 'All'): 11.203938484191895,
  ('200m', '[0.0, 90.0['): 9.4468526840209961,
  ('200m', '[180.0, 270.0['): 12.706465721130371,
  ('200m', '[270.0, 360.0]'): 10.000725984573364,
  ('200m', '[90.0, 180.0['): 10.862814903259277,
  ('20m', 'All'): 6.179999828338623,
  ('20m', '[0.0, 90.0['): 4.9499998092651367,
  ('20m', '[180.0, 270.0['): 7.6100001335144043,
  ('20m', '[270.0, 360.0]'): 5.7100000381469727,
  ('20m', '[90.0, 180.0['): 5.2929890155792236,
  ('40m', 'All'): 7.0959796905517578,
  ('40m', '[0.0, 90.0['): 5.9702688455581665,
  ('40m', '[180.0, 270.0['): 8.4523344039916992,
  ('40m', '[270.0, 360.0]'): 6.8096575736999512,
  ('40m', '[90.0, 180.0['): 6.2155957221984863,
  ('80m', 'All'): 8.6509017944335938,
  ('80m', '[0.0, 90.0['): 7.5837295055389404,
  ('80m', '[180.0, 270.0['): 9.7384757995605469,
  ('80m', '[270.0, 360.0]'): 8.1105818748474121,
  ('80m', '[90.0, 180.0['): 8.2832918167114258},
 'count': {('10m', 'All'): 604656.0,
  ('10m', '[0.0, 90.0['): 119842.0,
  ('10m', '[180.0, 270.0['): 234074.0,
  ('10m', '[270.0, 360.0]'): 126376.0,
  ('10m', '[90.0, 180.0['): 124364.0,
  ('140m', 'All'): 604656.0,
  ('140m', '[0.0, 90.0['): 116374.0,
  ('140m', '[180.0, 270.0['): 243590.0,
  ('140m', '[270.0, 360.0]'): 135292.0,
  ('140m', '[90.0, 180.0['): 109400.0,
  ('200m', 'All'): 604656.0,
  ('200m', '[0.0, 90.0['): 113475.0,
  ('200m', '[180.0, 270.0['): 242157.0,
  ('200m', '[270.0, 360.0]'): 143254.0,
  ('200m', '[90.0, 180.0['): 105770.0,
  ('20m', 'All'): 604656.0,
  ('20m', '[0.0, 90.0['): 116748.0,
  ('20m', '[180.0, 270.0['): 235304.0,
  ('20m', '[270.0, 360.0]'): 126961.0,
  ('20m', '[90.0, 180.0['): 125643.0,
  ('40m', 'All'): 604656.0,
  ('40m', '[0.0, 90.0['): 120426.0,
  ('40m', '[180.0, 270.0['): 238381.0,
  ('40m', '[270.0, 360.0]'): 128104.0,
  ('40m', '[90.0, 180.0['): 117745.0,
  ('80m', 'All'): 604656.0,
  ('80m', '[0.0, 90.0['): 119330.0,
  ('80m', '[180.0, 270.0['): 239146.0,
  ('80m', '[270.0, 360.0]'): 133220.0,
  ('80m', '[90.0, 180.0['): 112960.0},
 'max': {('10m', 'All'): 25.920000076293945,
  ('10m', '[0.0, 90.0['): 14.369999885559082,
  ('10m', '[180.0, 270.0['): 25.920000076293945,
  ('10m', '[270.0, 360.0]'): 19.549999237060547,
  ('10m', '[90.0, 180.0['): 14.930000305175781,
  ('140m', 'All'): 35.195941925048828,
  ('140m', '[0.0, 90.0['): 22.86467170715332,
  ('140m', '[180.0, 270.0['): 35.195941925048828,
  ('140m', '[270.0, 360.0]'): 29.814235687255859,
  ('140m', '[90.0, 180.0['): 20.905771255493164,
  ('200m', 'All'): 34.877243041992188,
  ('200m', '[0.0, 90.0['): 24.561836242675781,
  ('200m', '[180.0, 270.0['): 34.877243041992188,
  ('200m', '[270.0, 360.0]'): 33.732143402099609,
  ('200m', '[90.0, 180.0['): 21.536584854125977,
  ('20m', 'All'): 27.689998626708984,
  ('20m', '[0.0, 90.0['): 15.589999198913574,
  ('20m', '[180.0, 270.0['): 27.689998626708984,
  ('20m', '[270.0, 360.0]'): 21.649999618530273,
  ('20m', '[90.0, 180.0['): 15.979999542236328,
  ('40m', 'All'): 29.387109756469727,
  ('40m', '[0.0, 90.0['): 17.693622589111328,
  ('40m', '[180.0, 270.0['): 29.387109756469727,
  ('40m', '[270.0, 360.0]'): 25.192754745483398,
  ('40m', '[90.0, 180.0['): 16.793560028076172,
  ('80m', 'All'): 32.280239105224609,
  ('80m', '[0.0, 90.0['): 20.743719100952148,
  ('80m', '[180.0, 270.0['): 32.280239105224609,
  ('80m', '[270.0, 360.0]'): 27.942413330078125,
  ('80m', '[90.0, 180.0['): 19.532955169677734},
 'mean': {('10m', 'All'): 4.1893915969076261,
  ('10m', '[0.0, 90.0['): 3.3417930869221379,
  ('10m', '[180.0, 270.0['): 5.1125810579269269,
  ('10m', '[270.0, 360.0]'): 3.7938859684522601,
  ('10m', '[90.0, 180.0['): 3.670476718299061,
  ('140m', 'All'): 7.8465228797278623,
  ('140m', '[0.0, 90.0['): 6.6866495964827086,
  ('140m', '[180.0, 270.0['): 8.9531109376609503,
  ('140m', '[270.0, 360.0]'): 7.2187838345351443,
  ('140m', '[90.0, 180.0['): 7.3927146469551381,
  ('200m', 'All'): 8.4738967491657924,
  ('200m', '[0.0, 90.0['): 7.0707511105622967,
  ('200m', '[180.0, 270.0['): 9.7955929565714968,
  ('200m', '[270.0, 360.0]'): 7.7549135952858128,
  ('200m', '[90.0, 180.0['): 7.9270609315399172,
  ('20m', 'All'): 4.8153452911920738,
  ('20m', '[0.0, 90.0['): 3.9108075360827947,
  ('20m', '[180.0, 270.0['): 5.8345712321566516,
  ('20m', '[270.0, 360.0]'): 4.3517044317594324,
  ('20m', '[90.0, 180.0['): 4.2155453833197427,
  ('40m', 'All'): 5.6803902578298446,
  ('40m', '[0.0, 90.0['): 4.8012498160193742,
  ('40m', '[180.0, 270.0['): 6.6519476987395914,
  ('40m', '[270.0, 360.0]'): 5.3629782195278182,
  ('40m', '[90.0, 180.0['): 4.9579161339061493,
  ('80m', 'All'): 6.8429105603612399,
  ('80m', '[0.0, 90.0['): 5.9149683147210084,
  ('80m', '[180.0, 270.0['): 7.8021544717360065,
  ('80m', '[270.0, 360.0]'): 6.424779540308954,
  ('80m', '[90.0, 180.0['): 6.2855045603079853},
 'min': {('10m', 'All'): 0.0,
  ('10m', '[0.0, 90.0['): 0.0,
  ('10m', '[180.0, 270.0['): 0.0,
  ('10m', '[270.0, 360.0]'): 0.0,
  ('10m', '[90.0, 180.0['): 0.0,
  ('140m', 'All'): 0.0,
  ('140m', '[0.0, 90.0['): 0.0,
  ('140m', '[180.0, 270.0['): 0.0,
  ('140m', '[270.0, 360.0]'): 0.0,
  ('140m', '[90.0, 180.0['): 0.0,
  ('200m', 'All'): 0.0,
  ('200m', '[0.0, 90.0['): 0.0,
  ('200m', '[180.0, 270.0['): 0.0,
  ('200m', '[270.0, 360.0]'): 0.0,
  ('200m', '[90.0, 180.0['): 0.0,
  ('20m', 'All'): 0.0,
  ('20m', '[0.0, 90.0['): 0.0,
  ('20m', '[180.0, 270.0['): 0.0,
  ('20m', '[270.0, 360.0]'): 0.0,
  ('20m', '[90.0, 180.0['): 0.0,
  ('40m', 'All'): 0.0,
  ('40m', '[0.0, 90.0['): 0.0,
  ('40m', '[180.0, 270.0['): 0.0,
  ('40m', '[270.0, 360.0]'): 0.0,
  ('40m', '[90.0, 180.0['): 0.0,
  ('80m', 'All'): 0.0,
  ('80m', '[0.0, 90.0['): 0.0,
  ('80m', '[180.0, 270.0['): 0.0,
  ('80m', '[270.0, 360.0]'): 0.0,
  ('80m', '[90.0, 180.0['): 0.0},
 'std': {('10m', 'All'): 2.4321994530033586,
  ('10m', '[0.0, 90.0['): 1.7153268584149644,
  ('10m', '[180.0, 270.0['): 2.8194581011555386,
  ('10m', '[270.0, 360.0]'): 2.1909571297061241,
  ('10m', '[90.0, 180.0['): 1.8334834361369423,
  ('140m', 'All'): 3.6272652696793761,
  ('140m', '[0.0, 90.0['): 2.8894363480141649,
  ('140m', '[180.0, 270.0['): 3.8302160204846252,
  ('140m', '[270.0, 360.0]'): 3.4038884427629861,
  ('140m', '[90.0, 180.0['): 3.463171121328295,
  ('200m', 'All'): 4.0834920171291111,
  ('200m', '[0.0, 90.0['): 3.246180377116834,
  ('200m', '[180.0, 270.0['): 4.2979603238677564,
  ('200m', '[270.0, 360.0]'): 3.7366849435738714,
  ('200m', '[90.0, 180.0['): 3.9631501181722597,
  ('20m', 'All'): 2.5956531035815531,
  ('20m', '[0.0, 90.0['): 1.8115698416394523,
  ('20m', '[180.0, 270.0['): 2.9884465540389979,
  ('20m', '[270.0, 360.0]'): 2.342034699432777,
  ('20m', '[90.0, 180.0['): 1.9553532925384289,
  ('40m', 'All'): 2.7650269372360587,
  ('40m', '[0.0, 90.0['): 1.9926422334110316,
  ('40m', '[180.0, 270.0['): 3.1345356834325013,
  ('40m', '[270.0, 360.0]'): 2.6327655213933481,
  ('40m', '[90.0, 180.0['): 2.1057487187047053,
  ('80m', 'All'): 3.1164449954856375,
  ('80m', '[0.0, 90.0['): 2.4419473697940042,
  ('80m', '[180.0, 270.0['): 3.3838903052504601,
  ('80m', '[270.0, 360.0]'): 3.017648294312663,
  ('80m', '[90.0, 180.0['): 2.707882324438323}}

是否有人知道如何重新索引此数据框以使索引级别按排序顺序?

由于

3 个答案:

答案 0 :(得分:2)

@unutbu是正确的,这里没有手工构建索引。

df = a.append(b)
df.index = MultiIndex.from_arrays([a.index.tolist()*(len(b) + 1), 
                                   ["all"] + b.index.tolist()    ] )
df

                 25%    50%    75%    count      max    mean   min  std  
10m all          2.43   3.71   5.5    6.047e+05  25.92  4.189  0    2.432
    0.0_90.0     2.08   3.058  4.37   1.198e+05  14.37  3.342  0    1.715
    180.0_270.0  2.99   4.64   6.82   2.341e+05  25.92  5.113  0    2.819
    270.0_360.0  2.12   3.4    5.13   1.264e+05  19.55  3.794  0    2.191
    90.0_180.0   2.368  3.401  4.696  1.244e+05  14.93  3.67   0    1.833

答案 1 :(得分:1)

import pandas as PD

a = PD.DataFrame({'25%': {'10m': 2.429999828338623},
                  '50%': {'10m': 3.7100000381469727},
                  '75%': {'10m': 5.5},
                  'count': {'10m': 604656.0},
                  'max': {'10m': 25.920000076293945},
                  'mean': {'10m': 4.1893915969076261},
                  'min': {'10m': 0.0},
                  'std': {'10m': 2.4321994530033586}})

index = PD.MultiIndex.from_arrays([['10m'],['all']], names = ['dist', 'angle'])
a.index = index

b = PD.DataFrame({'25%': {'0.0_90.0': 2.0799999237060547,
                          '180.0_270.0': 2.9900000095367432,
                          '270.0_360.0': 2.119999885559082,
                          '90.0_180.0': 2.3681280016899109},
                  '50%': {'0.0_90.0': 3.0579087734222412,
                          '180.0_270.0': 4.6399998664855957,
                          '270.0_360.0': 3.4000000953674316,
                          '90.0_180.0': 3.4006340503692627},
                  '75%': {'0.0_90.0': 4.369999885559082,
                          '180.0_270.0': 6.8199996948242188,
                          '270.0_360.0': 5.130000114440918,
                          '90.0_180.0': 4.6960808038711548},
                  'count': {'0.0_90.0': 119842.0,
                            '180.0_270.0': 234074.0,
                            '270.0_360.0': 126376.0,
                            '90.0_180.0': 124364.0},
                  'max': {'0.0_90.0': 14.369999885559082,
                          '180.0_270.0': 25.920000076293945,
                          '270.0_360.0': 19.549999237060547,
                          '90.0_180.0': 14.930000305175781},
                  'mean': {'0.0_90.0': 3.3417930869221379,
                           '180.0_270.0': 5.1125810579269269,
                           '270.0_360.0': 3.7938859684522601,
                           '90.0_180.0': 3.670476718299061},
                  'min': {'0.0_90.0': 0.0,
                          '180.0_270.0': 0.0,
                          '270.0_360.0': 0.0,
                          '90.0_180.0': 0.0},
                  'std': {'0.0_90.0': 1.7153268584149644,
                          '180.0_270.0': 2.8194581011555386,
                          '270.0_360.0': 2.1909571297061241,
                          '90.0_180.0': 1.8334834361369423}})

index = PD.MultiIndex.from_arrays(
    [['']*4,
     ['0.0_90.0','180.0_270.0','270.0_360.0','90.0_180.0']],
    names = ['dist', 'angle'])
b.index = index

PD.set_printoptions(precision = 3)
new_df = PD.concat([a,b])
print(new_df[['count','mean','std','min','max','25%','50%','75%']])

产量

                   count  mean   std  min    max   25%   50%   75%
dist angle                                                        
10m  all          604656  4.19  2.43    0  25.92  2.43  3.71  5.50
     0.0_90.0     119842  3.34  1.72    0  14.37  2.08  3.06  4.37
     180.0_270.0  234074  5.11  2.82    0  25.92  2.99  4.64  6.82
     270.0_360.0  126376  3.79  2.19    0  19.55  2.12  3.40  5.13
     90.0_180.0   124364  3.67  1.83    0  14.93  2.37  3.40  4.70

请注意,b的多索引上方为第一个索引使用了一个空字符串:例如('','0.0_90.0')。您可以通过定义

将其更改为('10m', '0.0_90.0')
index = PD.MultiIndex.from_arrays(
    [['10m']*4,
     ['0.0_90.0','180.0_270.0','270.0_360.0','90.0_180.0']],
    names = ['dist', 'angle'])
b.index = index

默认情况下打印方式相同,或者调用

PD.set_printoptions(multi_sparse = True)

答案 2 :(得分:1)

其他有趣的选择:

In [19]: result = pd.concat([a, b]).rename({'10m': 'all'})

In [20]: result.set_index(np.array(['10m'] * 5), append=True).swaplevel(0,1)
Out[20]: 
                  count  mean   std  min    max   25%   50%   75%
10m all          604656  4.19  2.43    0  25.92  2.43  3.71  5.50
    0.0_90.0     119842  3.34  1.72    0  14.37  2.08  3.06  4.37
    180.0_270.0  234074  5.11  2.82    0  25.92  2.99  4.64  6.82
    270.0_360.0  126376  3.79  2.19    0  19.55  2.12  3.40  5.13
    90.0_180.0   124364  3.67  1.83    0  14.93  2.37  3.40  4.70