鍍金池/ 問答/數據分析&挖掘  Python  HTML/ 怎樣復制得到一個一模一樣的dataframe?

怎樣復制得到一個一模一樣的dataframe?

我有一個dataframe a=pd.DataFrame({"A":[1,2,3,4,5,6]})
怎樣復制得到另一個一模一樣的dataframe b?
dataframe.copy()中deep是True和False有什么區(qū)別?

import pandas as pd
a=pd.DataFrame({"A":[1,2,3,4,5,6]})
b=a.copy()
print("a",id(a),a.columns)
print("b",id(b),b.columns)
b.columns=["B"]
print("a",id(a),a.columns)
print("b",id(b),b.columns)

結果為:
a 35432320 Index(['A'], dtype='object')
b 99956440 Index(['A'], dtype='object')
a 35432320 Index(['A'], dtype='object')
b 99956440 Index(['B'], dtype='object')
是不是這樣就生成了一個新的對象b與a的內容一模一樣?

回答
編輯回答
笨小蛋

用copy.deepcopy吧。

    Parameters
            ----------
    deep : boolean or string, default True
        Make a deep copy, including a copy of the data and the indices.
        With ``deep=False`` neither the indices or the data are copied.

        Note that when ``deep=True`` data is copied, actual python objects
        will not be copied recursively, only the reference to the object.
        This is in contrast to ``copy.deepcopy`` in the Standard Library,
        which recursively copies object data.
2017年10月23日 16:43
編輯回答
尛憇藌

要先了解DataFrame.copy()默認就是 deep 模式。所以。。。。你的標題不太準確。

2017年11月2日 09:24