0

When i assign dataframe to another dataframe, making changes to one dataframe affects another dataframe

Code:

interest_margin_data = initial_margin_data
interest_margin_data['spanReq'] = (interest_margin_data['spanReq']*interest_margin_data['currency'].map(interestrate_dict))/(360*100*interest_margin_data['currency'].map(currency_dict))
initial_margin_data['spanReq'] /=  initial_margin_data['currency'].map(currency_dict)

The second line changes the values in initial_margin_data as well. Why is this so? How to affect this?

lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • Because assigning one DataFrame to a second variable does not automatically create a copy. – cs95 Feb 21 '19 at 06:02

1 Answers1

1

Use .copy to create a separate dataframe in memory:

interest_margin_data = initial_margin_data.copy()

It creates a different object in memory, rather than just pointing to the same place.

This is done so if you create a "view" of the dataframe it does not require substantially extra memory. It can index it, and calculate using the source.

In your case however you do not want this.

AER
  • 1,549
  • 19
  • 37