0

私のインデックス作成スキルは標準に達しておらず、この問題に苦しんでいます。

私は次の設定をしています:

import pandas as pd
import numpy as np

index = pd.bdate_range('2012-1-1', periods=250)
df1 = pd.DataFrame(np.random.rand(250,4), index=index, columns=[1, 2, 3, 4])
df2 = pd.DataFrame(np.random.rand(250,4), index=index, columns=[1, 2, 3, 4])
df = pd.concat({'A': df1, 'B': df2}, axis=1)

group = df.groupby([lambda x: x.year, lambda x: x.month])

私は最大数がわかりました。私のグループ内の営業日 (つまり、(年、月) の組み合わせ) は 23 です:

In [257]: group.size().max()
Out[257]: 23

また、毎月の最初の営業日 (インデックス n=0) については、次のように統計を取得できます。

In [258]: group.nth(0).describe()
Out[258]: 
               A                                           B             \
               1          2          3          4          1          2   
count  12.000000  12.000000  12.000000  12.000000  12.000000  12.000000   
mean    0.541559   0.491684   0.354012   0.448284   0.353839   0.408020   
std     0.367662   0.242924   0.254447   0.248426   0.228194   0.220511   
min     0.021792   0.110715   0.067677   0.074719   0.097227   0.116947   
25%     0.144712   0.368966   0.144415   0.209418   0.189507   0.260863   
50%     0.646160   0.439860   0.233370   0.472696   0.214474   0.370281   
75%     0.865417   0.614928   0.587038   0.710450   0.529376   0.602299   
max     0.963938   0.912865   0.766722   0.750037   0.778580   0.776627   


               3          4  
count  12.000000  12.000000  
mean    0.434197   0.588980  
std     0.301113   0.287869  
min     0.004253   0.064859  
25%     0.262517   0.357484  
50%     0.350605   0.653136  
75%     0.676960   0.775588  
max     0.991661   0.990118  

私がやりたいことは、group.nth(n).describe() for n in range(23) を実行し、結果を次の形式で保存することです。

                 count      mean       std   min   25%   50%   75%   max
(col2, n, col1)    281 -0.004093  0.140578 -1.64 -0.04 -0.00  0.04  0.58

(col2, n, col1) のすべての組み合わせで、col2 は下位の列名 (1 ~ 4)、n は範囲 (23)、col1 は上位の列名 ('A' または 'B') です。

どんな助けでも大歓迎です-私はこれらの種類の操作を行う方法について多くを学びます. 私はそこにいくつかの道を得ました:

group.nth(0).describe().stack().T.stack()`

しかし、n を 22 まで繰り返すと、そのハッシュが作成されます。

ありがとうございました。

4

1 に答える 1