[英]Copying dataframes through variables vs copying through variables for columns
I have been playing with python and understanding the concept of copying a dataframe through the .copy function as opposed to just reassigning it to a variable. 我一直在玩python,并且了解通过.copy函数复制数据帧的概念,而不是仅仅将其重新分配给变量。
Let's say we have the following data frame: dfx: 假设我们有以下数据框:dfx:
Name Score1 Score2 Score3 Score4
0 Jack 10 Perfect 10 Perfect
1 Jill 10 10 10 Not Finished
2 Jane 20 10 10 5
3 Tom Not Finished 15 10 5
dfx2 = dfx.drop("Score1",axis = 1)
dfx2: dfx2:
Name Score2 Score3 Score4
0 Jack Perfect 10 Perfect
1 Jill 10 10 Not Finished
2 Jane 10 10 5
3 Tom 15 10 5
running dfx again still returns the original dataframe 再次运行dfx仍返回原始数据帧
Name Score1 Score2 Score3 Score4
0 Jack 10 Perfect 10 Perfect
1 Jill 10 10 10 Not Finished
2 Jane 20 10 10 5
3 Tom Not Finished 15 10 5
Shouldn't the reassignment cause the column "Score1" be dropped from the original dataset as well? 重新分配是否也应导致从原始数据集中删除“ Score1”列?
However, running the following: 但是,运行以下命令:
dfx3 = dfx
dfx3
Name Score1 Score2 Score3 Score4
0 Jack 10 Perfect 10 Perfect
1 Jill 10 10 10 Not Finished
2 Jane 20 10 10 5
3 Tom Not Finished 15 10 5
dfx3.loc[0,"Score4"] = "BAD"
dfx3
Name Score1 Score2 Score3 Score4
0 Jack 10 Perfect 10 BAD
1 Jill 10 10 10 Not Finished
2 Jane 20 10 10 5
3 Tom Not Finished 15 10 5
dfx
Name Score1 Score2 Score3 Score4
0 Jack 10 Perfect 10 BAD
1 Jill 10 10 10 Not Finished
2 Jane 20 10 10 5
3 Tom Not Finished 15 10 5
does cause the original dataset to be modified. 确实会导致原始数据集被修改。
Any explanation why a column drop does not modify the original dataset but an element change does change the original? 有什么解释为什么列删除不会修改原始数据集,但是元素更改会更改原始数据集? and seems like any change to a column name in an assigned dataset also modifies the original dataset. 并且似乎对分配的数据集中的列名进行的任何更改也会修改原始数据集。
你引用dfx3
和dfx
一个数据帧,如果你想要做的操作上dfx3
有类似列dfx
的,那么你应该做的副本dfx
上dfx3
不是他们两个参考相同数据帧。
dfx3 = dfx.copy()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.