1. <small id='E7zKR'></small><noframes id='E7zKR'>

      <tfoot id='E7zKR'></tfoot>
      <legend id='E7zKR'><style id='E7zKR'><dir id='E7zKR'><q id='E7zKR'></q></dir></style></legend>
        <bdo id='E7zKR'></bdo><ul id='E7zKR'></ul>
      <i id='E7zKR'><tr id='E7zKR'><dt id='E7zKR'><q id='E7zKR'><span id='E7zKR'><b id='E7zKR'><form id='E7zKR'><ins id='E7zKR'></ins><ul id='E7zKR'></ul><sub id='E7zKR'></sub></form><legend id='E7zKR'></legend><bdo id='E7zKR'><pre id='E7zKR'><center id='E7zKR'></center></pre></bdo></b><th id='E7zKR'></th></span></q></dt></tr></i><div id='E7zKR'><tfoot id='E7zKR'></tfoot><dl id='E7zKR'><fieldset id='E7zKR'></fieldset></dl></div>

      1. Pandas 数据框和字典的深拷贝

        时间:2023-08-31

                  <bdo id='xouvi'></bdo><ul id='xouvi'></ul>
                  <i id='xouvi'><tr id='xouvi'><dt id='xouvi'><q id='xouvi'><span id='xouvi'><b id='xouvi'><form id='xouvi'><ins id='xouvi'></ins><ul id='xouvi'></ul><sub id='xouvi'></sub></form><legend id='xouvi'></legend><bdo id='xouvi'><pre id='xouvi'><center id='xouvi'></center></pre></bdo></b><th id='xouvi'></th></span></q></dt></tr></i><div id='xouvi'><tfoot id='xouvi'></tfoot><dl id='xouvi'><fieldset id='xouvi'></fieldset></dl></div>

                  <small id='xouvi'></small><noframes id='xouvi'>

                    <tbody id='xouvi'></tbody>

                • <tfoot id='xouvi'></tfoot>
                • <legend id='xouvi'><style id='xouvi'><dir id='xouvi'><q id='xouvi'></q></dir></style></legend>
                  本文介绍了Pandas 数据框和字典的深拷贝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm creating a small Pandas dataframe:

                  df = pd.DataFrame(data={'colA': [["a", "b", "c"]]})
                  

                  I take a deepcopy of that df. I'm not using the Pandas method but general Python, right?

                  import copy
                  df_copy = copy.deepcopy(df)
                  

                  A df_copy.head() gives the following:

                  Then I put these values into a dictionary:

                  mydict = df_copy.to_dict()
                  

                  That dictionary looks like this:

                  Finally, I remove one item of the list:

                  mydict['colA'][0].remove("b")
                  

                  I'm surprized that the values in df_copy are updated. I'm very confused that the values in the original dataframe are updated too! Both dataframes look like this now:

                  I understand Pandas doesn't really do deepcopy, but this wasn't a Pandas method. My questions are:

                  1) how can I build a dictionary from a dataframe that doesn't update the dataframe?

                  2) how can I take a copy of a dataframe which would be completely independent?

                  thanks for your help!

                  Cheers, Nicolas

                  解决方案

                  Disclaimer


                  Notice that putting mutable objects inside a DataFrame can be an antipattern so make sure that you really need it and you understand what you are doing.

                  Why doesn't your copy independent


                  When applied on an object, copy.deepcopy is looked up for a _deepcopy_ method of that object, that is called in turn. It's added to avoid copying too much for objects. In the case of a DataFrame instance in version 0.20.0 and above - _deepcopy_ doesn`t work recursively.

                  Similarly, if you will use DataFrame.copy(deep=True) deep copy will copy the data, but will not do so recursively. .

                  How to solve the problem


                  To take a truly deep copy of a DataFrame containing a list(or other python objects), so that it will be independent - you can use one of the methods below.

                  df_copy = pd.DataFrame(columns = df.columns, data = copy.deepcopy(df.values))
                  

                  For a dictionary, you may use same trick:

                  mydict = pd.DataFrame(columns = df.columns, data = copy.deepcopy(df_copy.values)).to_dict()
                  mydict['colA'][0].remove("b")
                  

                  There's also a standard hacky way of deep-copying python objects:

                  import pickle
                  df_copy = pickle.loads(pickle.dumps(df))  
                  

                  Feel free to ask for any clarifications, if needed.

                  这篇关于Pandas 数据框和字典的深拷贝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:克服 Python 关于实例方法的限制 下一篇:将 Pandas DataFrame 切片成新的 DataFrame

                  相关文章

                  <i id='jqEig'><tr id='jqEig'><dt id='jqEig'><q id='jqEig'><span id='jqEig'><b id='jqEig'><form id='jqEig'><ins id='jqEig'></ins><ul id='jqEig'></ul><sub id='jqEig'></sub></form><legend id='jqEig'></legend><bdo id='jqEig'><pre id='jqEig'><center id='jqEig'></center></pre></bdo></b><th id='jqEig'></th></span></q></dt></tr></i><div id='jqEig'><tfoot id='jqEig'></tfoot><dl id='jqEig'><fieldset id='jqEig'></fieldset></dl></div>

                  <legend id='jqEig'><style id='jqEig'><dir id='jqEig'><q id='jqEig'></q></dir></style></legend>

                      <bdo id='jqEig'></bdo><ul id='jqEig'></ul>
                  1. <tfoot id='jqEig'></tfoot>

                    <small id='jqEig'></small><noframes id='jqEig'>