<bdo id='Tj8Ce'></bdo><ul id='Tj8Ce'></ul>

    1. <legend id='Tj8Ce'><style id='Tj8Ce'><dir id='Tj8Ce'><q id='Tj8Ce'></q></dir></style></legend>

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

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

        将 Pandas DataFrame 切片成新的 DataFrame

        时间:2023-08-31

            • <small id='kRlTM'></small><noframes id='kRlTM'>

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

                  本文介绍了将 Pandas DataFrame 切片成新的 DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想用布尔索引对 DataFrame 进行切片以获得副本,然后在该副本上独立于原始 DataFrame 执行操作.

                  I would like to slice a DataFrame with a Boolean index obtaining a copy, and then do stuff on that copy independently of the original DataFrame.

                  从这个 answer 判断,使用布尔数组使用 .loc 进行选择会给我返回一个副本,但是,如果我尝试更改副本,SettingWithCopyWarning 会妨碍.那么这是否是正确的方法:

                  Judging from this answer, selecting with .loc using a Boolean array will hand me back a copy, but then, if I try to change the copy, SettingWithCopyWarning gets in the way. Would this then be the correct way:

                  import numpy as np
                  import pandas as pd
                  d1 = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e'])
                  # create a new dataframe from the sliced copy
                  d2 = pd.DataFrame(d1.loc[d1.a > 1, :])
                  # do stuff with d2, keep d1 unchanged
                  

                  推荐答案

                  你需要copyboolean indexing,不需要新的DataFrame构造函数:

                  d2 = d1[d1.a > 1].copy()
                  

                  警告说明:

                  如果您稍后修改 d2 中的值,您会发现修改不会传播回原始数据 (d1),并且 Pandas 会发出警告.

                  If you modify values in d2 later you will find that the modifications do not propagate back to the original data (d1), and that Pandas does warning.

                  这篇关于将 Pandas DataFrame 切片成新的 DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Pandas 数据框和字典的深拷贝 下一篇:嵌套字典 copy() 还是 deepcopy()?

                  相关文章

                      <tfoot id='qDyKx'></tfoot>

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

                    2. <legend id='qDyKx'><style id='qDyKx'><dir id='qDyKx'><q id='qDyKx'></q></dir></style></legend>
                      • <bdo id='qDyKx'></bdo><ul id='qDyKx'></ul>