1

I am trying to rename the key and subkey in python nested dictionary. However, I haven't got the result that I expected yet. Below is the original nested key that I have.

nested_dict = {
 0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
 1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
 2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
 3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}
 }

I am trying to change the key and subkey to another value into this.

 nested_dict = {
 4: {4: 33.97, 5: 55.32, 6: 57.31, 7: 71.56},
 5: {4: 27.31, 5: 23.32, 6: 32.25, 7: 60.21},
 6: {4: 65.38, 5: 36.88, 6: 70.88, 7: 21.93},
 7: {4: 35.44, 5: 21.21, 6: 40.72, 7: 51.35}
 }

What I have in mind is renaming the key using a list. I have tried to replace the key and subkey with a list below:

new_key = []
for i in range(4,8):
    new_key.append(i)

However, I still haven't got it. Another idea is using pandas DataFrame to rename both key and subkey. I am not sure whether using lists or pandas is suitable for the given problem.

mrani
  • 21
  • 5

3 Answers3

0

Code for renaming a key from here:

mydict[new_key] = mydict.pop(old_key)
keiv.fly
  • 3,343
  • 4
  • 26
  • 45
0

You could use a (nested) dict comprehension ([Python]: PEP 274 -- Dict Comprehensions). Note that it generates a new dictionary (but you can assign it to the old variable):

>>> from pprint import pprint as pp
>>>
>>> nested_dict = {
...  0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
...  1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
...  2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
...  3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}
...  }
>>>
>>> pp(nested_dict)
{0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
 1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
 2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
 3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}}
>>>
>>> modified_nested_dict = {k0 + 4: {k1 + 4: v1 for k1, v1 in v0.items()} for k0, v0 in nested_dict.items()}
>>>
>>> pp(modified_nested_dict)
{4: {4: 33.97, 5: 55.32, 6: 57.31, 7: 71.56},
 5: {4: 27.31, 5: 23.32, 6: 32.25, 7: 60.21},
 6: {4: 65.38, 5: 36.88, 6: 70.88, 7: 21.93},
 7: {4: 35.44, 5: 21.21, 6: 40.72, 7: 51.35}}
CristiFati
  • 38,250
  • 9
  • 50
  • 87
0

You can use Pandas Dataframe for the desired task, as follows:

import pandas as pd
nested_dict = {
            0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
            1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
            2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
            3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}
            }
print("Dictionary before renaming: ", nested_dict)

# Convert nested dictionary to Pandas Dataframe
my_dataframe = pd.DataFrame.from_dict(nested_dict)

new_keys = list(range(4, 8)) # List of new keys
my_dataframe.columns = new_keys # Set columns to the new keys
my_dataframe.set_index([new_keys], inplace=True) # Set index to the new keys
nested_dict = my_dataframe.to_dict() # Convert back to nested dictionary

print("Dictionary after  renaming: ", nested_dict)

This gives you the following expected output:

Dictionary before renaming:  {0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56}, 1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21}, 2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93}, 3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}}
Dictionary after  renaming:  {4: {4: 33.97, 5: 55.32, 6: 57.31, 7: 71.56}, 5: {4: 27.31, 5: 23.32, 6: 32.25, 7: 60.21}, 6: {4: 65.38, 5: 36.88, 6: 70.88, 7: 21.93}, 7: {4: 35.44, 5: 21.21, 6: 40.72, 7: 51.35}}
Frida Schenker
  • 1,219
  • 1
  • 9
  • 14