Skip to content
Advertisement

Misunderstanding of global variable in Python

I would like to calculate the variable “a” by using a function and the global variable “df”. My problem is that after running the function, “df” is also altered. I just want to calculate “a” with the function, but I want that “df” stays as it is.

JavaScript

actual result:

JavaScript

expected result:

JavaScript

Advertisement

Answer

when you are assiging a = df. they are referencing to same thing. So when you’re changing some property in a, the df also gets changed. As you do not want to change df inside function, just use copy() and work with the copy. Inside fun(), do:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement