如何在嵌套字典中按元素访问pandas multiindex?

时间:2016-09-14 05:59:07

标签: python pandas dictionary multi-index

我有一个区域类型的字典,在每个子区域的字典中,并且在每个区域内都有一个pandas数据帧对象,索引到我需要计算每个参数(列)时间序列的时间段。另外,我需要两个单元。

所以我创造了这样的东西:

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}}

现在我需要从每天提取一个值(在这里使用np.random)并以某种方式将其插入到适当的位置。我已成功进入单嵌套dict并更新DataFrame对象(使用dict_[key].loc[date] = x),但此处的“类似”方法返回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

它只是返回我开始的东西。我非常感谢有关如何更新这些值的任何建议。请原谅凌乱的代码,这是我写的最简单的,可以重现我(更丑陋)的问题。

1 个答案:

答案 0 :(得分:2)

loc中指定索引和列 尝试

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