首先,我创建一个 DataFrame
In [61]: import pandas as pd
In [62]: df = pd.DataFrame([[1], [2], [3]])
然后,我通过copy
In [63]: df2 = df.copy(deep=True)
现在 DataFrame
不同了。
In [64]: id(df), id(df2)
Out[64]: (4385185040, 4385183312)
但是,index
还是一样的。
In [65]: id(df.index), id(df2.index)
Out[65]: (4385175264, 4385175264)
同样的事情发生在列中,有什么方法可以轻松地深度复制它,不仅是值,还包括索引和列?
最佳答案
最新版的 Pandas 没有这个问题了
import pandas as pd
df = pd.DataFrame([[1], [2], [3]])
df2 = df.copy(deep=True)
id(df), id(df2)
Out[3]: (136575472, 127792400)
id(df.index), id(df2.index)
Out[4]: (145820144, 127657008)
关于python - 在 pandas 中,我可以深度复制包含索引和列的 DataFrame 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17591104/