Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

debugging - Python variable, in a Jupyter notebook, modified despite no further action to it

I'm not going to share the direct code since I feel the length of it would bog down anyone's analysis of my main question (though I can share it if absolutely need be). It's best I summarize it.

To put it generally, in a previous cell, I established a pandas dataframe - let's say we call it original_df here. In the next cell, I initialize a new variable temp_df = original_df so that I may manipulate the same data while keeping the original dataframe in tact. At no point in the code that follows do I ever once assign anything to nor even use original_df in any way, yet when I get done manipulating temp_df as much as I need to...subsequently checking original_df shows that it has changed in all the same ways that temp_df did.

Any ideas why this would happen?, some type of environment issue perhaps? Re-running each of the code cells that created original_df is rather inconvenient, and putting all of the code in one cell would negate the whole point of using a Jupyter Notebook (given the visuals along the way). I find it incredibly bizarre that this is happening at all, but is there any way to explicitly force a "freeze" on the original_df variable while its temp_df copy is being manipulated?

question from:https://stackoverflow.com/questions/65642314/python-variable-in-a-jupyter-notebook-modified-despite-no-further-action-to-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could try to copy the dataframe with the libary copy

import copy

temp_df = copy.copy(original_df)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...