Search code examples
pythonjsondictionaryoverwrite

Updating the values of a python dictionary key by iterating over a data frame without overwriting existing values


I want to create a dictionary using the values in a data frame (taken from a csv file) and values defined in the code. Then need to write that into a json file. Below is my code and expected output. I want to update the values related to a key keeping the existing values.

import json
import os.path
import pandas as pd

df = pd.read_csv('country.csv')

diction = {}

for index, row in df.iterrows():
    a = "country_details"
    u = "global"
    g = str(row['name'])
    h = str(row['country_code'])
    i = str(row['region'])

    diction.update({
        "initial_configurations":
            {
                g: [
                    [f"{a}", f"{g}"],
                    [f"t_s{u}", f"{h}"]]
            },
        "final_configurations":
            {
                g: [
                    [f"{a}", f"{g}"],
                    [f"t_s{u}", f"{h}"]]
            },
    })

with open('web.json', 'a', encoding='utf-8') as file:
    # for row in df:
    json.dump(diction, file, ensure_ascii=False)

link to csv file - https://drive.google.com/file/d/10AHu-njt2AIDFe3j5BPVJcENKqh_3Uck/view?usp=share_link

I'm getting below,

{"initial_configurations": {"Qatar": [["country_details", "Qatar"], ["t_sglobal", "QA"]]}, "final_configurations": {"Qatar": [["country_details", "Qatar"], ["t_sglobal", "QA"]]}}

But I want to get below, Not only the last value Quatar, I need to have other values under 'name' in the date frame without overwriting

{"initial_configurations":{"Australia": [["country_details", "Australia"], ["t_sglobal", "AU"]],[["country_details", "Bangladesh"], ["t_sglobal", "BD"]]....},
"final_configurations": {"Australia": [["country_details", "Australia"], ["t_sglobal", "AU"]],[["country_details", "Bangladesh"], ["t_sglobal", "BD"]]...}}

Solution

  • You are overriding the initial_configurations and final_configurations keys every iteration, you need to append values to those keys

    diction = {'initial_configurations': dict(), 'final_configurations': dict()}
    
    for index, row in df.iterrows():
        a = "country_details"
        u = "global"
        g = str(row['name'])
        h = str(row['country_code'])
    
        diction['initial_configurations'].update({g: [[f"{a}", f"{g}"], [f"t_s{u}", f"{h}"]]})
        diction['final_configurations'].update({g: [[f"{a}", f"{g}"], [f"t_s{u}", f"{h}"]]})
    

    web.json:

    {"initial_configurations": {"Australia": [["country_details", "Australia"], ["t_sglobal", "AU"]], "Bangladesh": [["country_details", "Bangladesh"], ["t_sglobal", "BD"]], "India": [["country_details", "India"], ["t_sglobal", "IND"]], "New Zealand": [["country_details", "New Zealand"], ["t_sglobal", "NZ"]], "Norway": [["country_details", "Norway"], ["t_sglobal", "NO"]], "Poland": [["country_details", "Poland"], ["t_sglobal", "PL"]], "Qatar": [["country_details", "Qatar"], ["t_sglobal", "QA"]]},
     "final_configurations": {"Australia": [["country_details", "Australia"], ["t_sglobal", "AU"]], "Bangladesh": [["country_details", "Bangladesh"], ["t_sglobal", "BD"]], "India": [["country_details", "India"], ["t_sglobal", "IND"]], "New Zealand": [["country_details", "New Zealand"], ["t_sglobal", "NZ"]], "Norway": [["country_details", "Norway"], ["t_sglobal", "NO"]], "Poland": [["country_details", "Poland"], ["t_sglobal", "PL"]], "Qatar": [["country_details", "Qatar"], ["t_sglobal", "QA"]]}}