python - 如何按嵌套字典中的元素访问 pandas 多重索引?

标签 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)并以某种方式将其插入到正确的位置。我已经成功进入单嵌套字典并更新 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

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

最佳答案

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

关于python - 如何按嵌套字典中的元素访问 pandas 多重索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39483417/

相关文章:

python - 定位嵌套节点组失败并显示 "Minion did not return. [No response]",尽管主机响应如果通过其他标准定位

python - 删除与另一个数据帧交叉处丢失的数据帧行

python - 存储为 Pandas DataFrames 并更新为 Pytables

javascript - 隐藏标记 - Mapbox

excel - 更改字典中集合中项目的值

java - 第 n 次调用函数

python - Pandas 向前填充破坏顺序的列部分

python - 根据列子文本有条件地更改列数据类型

python - Pandas:将数据写入 MySQL 时减少了毫秒数

Python 和 Pandas : How to return a copy of a dataframe?