首页 > 解决方案 > 从数据框中更新嵌套字典值

问题描述

我正在寻找从熊猫数据框中更新嵌套字典值,但目前它更新所有嵌套字典,而不仅仅是所需的字典。这是我当前的代码 -

import pprint
import pandas as pd

update_dict = [
    [1, 10],
    [1, 30],
    [2, 20],
    [2, 40],
               ]

update_df = pd.DataFrame(update_dict, columns=['list_one', 'list_two'])

list_one = [1, 2, 3, 4, 5]
list_two = [10, 20, 30, 40]

dictionary = {}
sub_dictionary = dict.fromkeys(list_two, 0)
for item in list_one:
    dictionary[item] = sub_dictionary

for item in list_one:
    sub_df = update_df[update_df['list_one'] == item]
    sub_list = sorted(list(set(sub_df['list_two'].to_list())))

    for sub_item in sub_list:
        dictionary[item][sub_item] = 1

原始字典看起来像这样 -

{1: {10: 0, 20: 0, 30: 0, 40: 0},
 2: {10: 0, 20: 0, 30: 0, 40: 0},
 3: {10: 0, 20: 0, 30: 0, 40: 0},
 4: {10: 0, 20: 0, 30: 0, 40: 0},
 5: {10: 0, 20: 0, 30: 0, 40: 0}}

目前输出看起来像这样 -

{1: {10: 1, 20: 1, 30: 1, 40: 1},
 2: {10: 1, 20: 1, 30: 1, 40: 1},
 3: {10: 1, 20: 1, 30: 1, 40: 1},
 4: {10: 1, 20: 1, 30: 1, 40: 1},
 5: {10: 1, 20: 1, 30: 1, 40: 1}}

我希望输出看起来像这样 -

{1: {10: 1, 20: 0, 30: 1, 40: 0},
 2: {10: 0, 20: 1, 30: 0, 40: 1},
 3: {10: 0, 20: 0, 30: 0, 40: 0},
 4: {10: 0, 20: 0, 30: 0, 40: 0},
 5: {10: 0, 20: 0, 30: 0, 40: 0}}

任何帮助将不胜感激。谢谢

标签: pythonpandas

解决方案


您正在使用相同的sub_dictionary,只需执行以下操作:

dictionary = {}
for item in list_one:
    dictionary[item] = dict.fromkeys(list_two, 0)

输出

{1: {10: 1, 20: 0, 30: 1, 40: 0},
 2: {10: 0, 20: 1, 30: 0, 40: 1},
 3: {10: 0, 20: 0, 30: 0, 40: 0},
 4: {10: 0, 20: 0, 30: 0, 40: 0},
 5: {10: 0, 20: 0, 30: 0, 40: 0}}

当你这样做时:

dictionary = {}
sub_dictionary = dict.fromkeys(list_two, 0)
for item in list_one:
    dictionary[item] = sub_dictionary

字典中的所有值都将指向同一个字典,因此当您更新任何值时,更改会反映在所有值中。

提供的答案将为每个值创建一个新字典。


推荐阅读