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

  2. <small id='6SV3x'></small><noframes id='6SV3x'>

    1. <tfoot id='6SV3x'></tfoot>

      Pandas:链式作业

      Pandas: Chained assignments(Pandas:链式作业)

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

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

              <tbody id='DtjqX'></tbody>
            • <bdo id='DtjqX'></bdo><ul id='DtjqX'></ul>
              <tfoot id='DtjqX'></tfoot>
            • <i id='DtjqX'><tr id='DtjqX'><dt id='DtjqX'><q id='DtjqX'><span id='DtjqX'><b id='DtjqX'><form id='DtjqX'><ins id='DtjqX'></ins><ul id='DtjqX'></ul><sub id='DtjqX'></sub></form><legend id='DtjqX'></legend><bdo id='DtjqX'><pre id='DtjqX'><center id='DtjqX'></center></pre></bdo></b><th id='DtjqX'></th></span></q></dt></tr></i><div id='DtjqX'><tfoot id='DtjqX'></tfoot><dl id='DtjqX'><fieldset id='DtjqX'></fieldset></dl></div>
                本文介绍了Pandas:链式作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我一直在阅读这个 链接 关于返回视图与副本".我真的不明白 Pandas 中的 chained assignment 概念是如何工作的,以及 .ix().iloc().loc() 会影响它.

                I have been reading this link on "Returning a view versus a copy". I do not really get how the chained assignment concept in Pandas works and how the usage of .ix(), .iloc(), or .loc() affects it.

                我收到以下代码行的 SettingWithCopyWarning 警告,其中 data 是 Panda 数据框,amount 是一列(系列)该数据框中的名称:

                I get the SettingWithCopyWarning warnings for the following lines of codes, where data is a Panda dataframe and amount is a column (Series) name in that dataframe:

                data['amount'] = data['amount'].astype(float)
                
                data["amount"].fillna(data.groupby("num")["amount"].transform("mean"), inplace=True)
                
                data["amount"].fillna(mean_avg, inplace=True)
                

                看看这段代码,很明显我在做一些次优的事情吗?如果是这样,你能告诉我替换代码行吗?

                Looking at this code, is it obvious that I am doing something suboptimal? If so, can you let me know the replacement code lines?

                我知道以下警告,并认为我的情况下的警告是误报:

                I am aware of the below warning and like to think that the warnings in my case are false positives:

                链式分配警告/异常旨在通知可能无效分配的用户.可能有误报;无意中报告了链式分配的情况.

                The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid assignment. There may be false positives; situations where a chained assignment is inadvertantly reported.

                导致第一次复制警告错误的代码.

                EDIT : the code leading to the first copy warning error.

                data['amount'] = data.apply(lambda row: function1(row,date,qty), axis=1) 
                data['amount'] = data['amount'].astype(float)
                
                def function1(row,date,qty):
                    try:
                        if(row['currency'] == 'A'):
                            result = row[qty]
                        else:
                            rate = lookup[lookup['Date']==row[date]][row['currency'] ]
                            result = float(rate) * float(row[qty])
                        return result
                    except ValueError: # generic exception clause
                        print "The current row causes an exception:"
                

                推荐答案

                SettingWithCopy 的重点是警告用户您可能正在做一些不会正如人们所期望的那样更新原始数据框.

                The point of the SettingWithCopy is to warn the user that you may be doing something that will not update the original data frame as one might expect.

                这里,data 是一个数据帧,可能是单个 dtype(或不是).然后,您将引用这个 data['amount'] 这是一个系列,并更新它.这可能适用于您的情况,因为您返回的数据类型与现有数据相同.

                Here, data is a dataframe, possibly of a single dtype (or not). You are then taking a reference to this data['amount'] which is a Series, and updating it. This probably works in your case because you are returning the same dtype of data as existed.

                但是它可以创建一个副本来更新您看不到的 data['amount'] 的副本;然后你会想知道为什么它没有更新.

                However it could create a copy which updates a copy of data['amount'] which you would not see; Then you would be wondering why it is not updating.

                Pandas 几乎在所有方法调用中都会返回一个对象的副本.inplace 操作是一种可行的操作,但通常不清楚是否正在修改数据并可能在副本上工作.

                Pandas returns a copy of an object in almost all method calls. The inplace operations are a convience operation which work, but in general are not clear that data is being modified and could potentially work on copies.

                这样做更清楚:

                data['amount'] = data["amount"].fillna(data.groupby("num")["amount"].transform("mean"))
                
                data["amount"] = data['amount'].fillna(mean_avg)
                

                制作副本的另一个优点.您可以链接操作,这对于 inplace 是不可能的.

                One further plus to working on copies. You can chain operations, this is not possible with inplace ones.

                例如

                data['amount'] = data['amount'].fillna(mean_avg)*2
                

                仅供参考.inplace 操作既不是更快也不是内存效率更高.my2c 他们应该被禁止.但该 API 为时已晚.

                And just an FYI. inplace operations are neither faster nor more memory efficient. my2c they should be banned. But too late on that API.

                你当然可以关闭它:

                pd.set_option('chained_assignment',None)
                

                Pandas 与整个测试套件一起运行,此设置为 raise(因此我们知道是否正在发生链接),仅供参考.

                Pandas runs with the entire test suite with this set to raise (so we know if chaining is happening) on, FYI.

                这篇关于Pandas:链式作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                      <legend id='0EXmm'><style id='0EXmm'><dir id='0EXmm'><q id='0EXmm'></q></dir></style></legend>

                        <tbody id='0EXmm'></tbody>
                        <bdo id='0EXmm'></bdo><ul id='0EXmm'></ul>

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

                        1. <small id='0EXmm'></small><noframes id='0EXmm'>

                          <tfoot id='0EXmm'></tfoot>