2016-09-14 3 views
1

私は、それぞれのパラメータ(列)の時系列を計算する必要がある期間に索引付けされた、サブ領域の各ディクショナリ内の、およびそれらの各パンダデータフレームオブジェクト内の領域のタイプの辞書を持っています。さらに、私は2つのユニットでそれが必要です。入れ子にされたdictの中の要素によってpandas multiindexにアクセスするには?

だから私はこのようなものを作成しました:今、私は(ここnp.randomを使用して)毎日から値を抽出する必要があり、何とかそれが適切な場所だにすることを挿入

regions = ['region_x', 'region_y'] 
sub_regions = ['a', 'b', 'c'] 
parameters = ['x', 'y', 'z'] 
units = ['af', 'cbm'] 
start = datetime(2000, 01, 01) 
end = datetime(2000, 01, 03) 

arrays = [parameters * 2, units * 3] 

cols = pd.MultiIndex.from_arrays(arrays) 
empty_df = pd.DataFrame(index=pd.date_range(start, end), columns=cols).fillna(0.0) 

tab_dict = {} 
for region in regions: 
    tab_dict.update({region: {}}) 
    for sub_region in sub_regions: 
     tab_dict[region].update({sub_region: empty_df}) 

{'region_y': 
{'a':  x y z x y z 
      af cbm af cbm af cbm 
2000-01-01 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-02 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-03 0.0 0.0 0.0 0.0 0.0 0.0, 
'c':   x y z x y z 
      af cbm af cbm af cbm 
2000-01-01 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-02 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-03 0.0 0.0 0.0 0.0 0.0 0.0, 
'b':  x y z x y z 
      af cbm af cbm af cbm 
2000-01-01 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-02 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-03 0.0 0.0 0.0 0.0 0.0 0.0}, 
'region_x': 
{'a':  x y z x y z 
      af cbm af cbm af cbm 
2000-01-01 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-02 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-03 0.0 0.0 0.0 0.0 0.0 0.0, 
'c':   x y z x y z 
      af cbm af cbm af cbm 
2000-01-01 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-02 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-03 0.0 0.0 0.0 0.0 0.0 0.0, 
'b':   x y z x y z 
      af cbm af cbm af cbm 
2000-01-01 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-02 0.0 0.0 0.0 0.0 0.0 0.0 
2000-01-03 0.0 0.0 0.0 0.0 0.0 0.0}} 

を返します。 。私は単一のネストされたdictと(dict_[key].loc[date] = xを使用して)DataFrameオブジェクトを更新することに成功しましたが、ここで '類似の'アプローチはSettingWithCopyWarningを返し、データフレームを更新しません。

for day in rrule.rrule(rrule.DAILY, dtstart=start, until=end): 
    for region in regions: 
     for sub_region in sub_regions: 
      for parameter in parameters: 
       for unit in units: 
        unit_af = np.random.randint(100) 
        unit_cbm = unit_af * 2 
        tab_dict[region][sub_region][parameter]['af'].loc[day] = unit_af 
        tab_dict[region][sub_region][parameter]['cbm'].loc[day] = unit_cbm 

これはちょうど私が始めたものを返します。これらの値を更新する方法に関するアドバイスをいただければ幸いです。間違ったコードを許して、これは私の(もっと醜い)問題を再現するために書くことができる最も簡単なものでした。

答えて

2

for day in rrule.rrule(rrule.DAILY, dtstart=start, until=end): 
    for region in regions: 
     for sub_region in sub_regions: 
      for parameter in parameters: 
       for unit in units: 
        unit_af = np.random.randint(100) 
        unit_cbm = unit_af * 2 
        tab_dict[region][sub_region][parameter].loc[day, 'af'] = unit_af 
        tab_dict[region][sub_region][parameter].loc[day, 'cbm'] = unit_cbm 
を試してみてください loc
にインデックスと列の両方を指定します
関連する問題