Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dict of DataFrames

Let's say I initialize a df and then I assign it to a dict 3 times, each one with a specific key.

import pandas as pd

df = pd.DataFrame({'A': [2, 2], 'B': [2, 2]})

dict = {}

for i in range(3):
    dict_strat['Df {0}'.format(i)] = df

Alright, what I'm not understanding is that when I try to change value of one element in the dictionary, it changes all the others. For example:

dict_strat['Df 0'].iloc[0, :] = 9

It not only changes the first df on the dict, it changes all of them. Why? How can I get rid of that?

like image 340
Davi Avatar asked May 26 '26 02:05

Davi


1 Answers

The DataFrames are all shallow copies, meaning that mutating one of them will mutate the others in the dictionary.

To resolve this issue, make deep copies using .copy(). You also should be using f-strings rather than .format():

for i in range(3):
    dict_strat[f'Df {i}'] = df.copy()
like image 171
BrokenBenchmark Avatar answered May 28 '26 15:05

BrokenBenchmark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!