Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why doesn't dictionary value change accordingly in the original dict when it's modified?

import pandas as pd

df1 = pd.DataFrame({
  "a": [420, 380, 390],
  "b": [50, 40, 45]
})

df2 = pd.DataFrame({
  "c": [420, 380, 390],
  "d": [50, 40, 45]
})

dfs = {'df1': df1, 'df2':df2}

for name, df in dfs.items():
    df = {}
    pass

print('dfs: ', dfs)

When I modify each df in the for loop, why isn’t this change updated to the dfs? I suppose the print() should print an empty dict, but it didn’t change at all. I have do an explicit reassignment to update dfs:

for name, df in dfs.items():
   df = {}
   dfs[name] = df

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df is a local variable defined each time for loop runs. So, it would behave like a new variable, and assigning a new value would not change the source (dfs). What you need is exactly as your second cell, but to have a better understanding, I have edited it a little bit:

import pandas as pd

df1 = pd.DataFrame({
  "a": [420, 380, 390],
  "b": [50, 40, 45]
})

df2 = pd.DataFrame({
  "c": [420, 380, 390],
  "d": [50, 40, 45]
})

dfs = {'df1': df1, 'df2':df2}

print('dfs before: ', dfs)
for name, df in dfs.items():
    dfs[name] = {}


print('dfs after: ', dfs)

Output

dfs before:  {'df1':      a   b
0  420  50
1  380  40
2  390  45, 'df2':      c   d
0  420  50
1  380  40
2  390  45}
dfs after:  {'df1': {}, 'df2': {}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading