python - 从数据框中更新嵌套字典值
问题描述
我正在寻找从熊猫数据框中更新嵌套字典值,但目前它更新所有嵌套字典,而不仅仅是所需的字典。这是我当前的代码 -
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}}
任何帮助将不胜感激。谢谢
解决方案
您正在使用相同的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
字典中的所有值都将指向同一个字典,因此当您更新任何值时,更改会反映在所有值中。
提供的答案将为每个值创建一个新字典。
推荐阅读
- python - 使用 pyodbc 将 pandas 数据帧高效插入到 MS SQL Server
- c# - 无法从 macOS 上的资源文件加载 .pem
- clojure - Clojure csv 映射为 {:key1 #{a, b, c}, :key2 #{n, m, k}}
- firebase - 我可以在自定义域中使用通配符子域吗?
- php - 如何在类中编写 MySQL 连接
- ios - 将当前二进制文件和 IAP 重新提交到 App Store?
- java - NumberFormat 货币解析失败“无法解析的数字”
- for-loop - 需要在循环中验证字符串的一半
- azure - Azure Powershell - 将多个服务终结点应用于子网
- json - 如何使 Laravel Eloquent 资源在链接中包含过滤器参数?