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 :
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': {}}