<bdo id='6CeZY'></bdo><ul id='6CeZY'></ul>

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

      <tfoot id='6CeZY'></tfoot>

      <small id='6CeZY'></small><noframes id='6CeZY'>

      <legend id='6CeZY'><style id='6CeZY'><dir id='6CeZY'><q id='6CeZY'></q></dir></style></legend>
    1. 使用条件语句替换 pandas DataFrame 中的条目

      Replace an entry in a pandas DataFrame using a conditional statement(使用条件语句替换 pandas DataFrame 中的条目)
        <tbody id='BBlrG'></tbody>

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

            <tfoot id='BBlrG'></tfoot>

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

              1. 本文介绍了使用条件语句替换 pandas DataFrame 中的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想在给定条件的情况下更改 Dataframe 中条目的值.例如:

                I'd like to change the value of an entry in a Dataframe given a condition. For instance:

                d = pandas.read_csv('output.az.txt', names = varname)
                d['uld'] = (d.trade - d.plg25)*(d.final - d.price25)
                
                if d['uld'] > 0:
                   d['uld'] = 1
                else:
                   d['uld'] = 0
                

                我不明白为什么上述方法不起作用.感谢您的帮助.

                I'm not understanding why the above doesn't work. Thank you for your help.

                推荐答案

                使用 np.where 根据简单的布尔标准设置数据:

                Use np.where to set your data based on a simple boolean criteria:

                In [3]:
                
                df = pd.DataFrame({'uld':np.random.randn(10)})
                df
                Out[3]:
                        uld
                0  0.939662
                1 -0.009132
                2 -0.209096
                3 -0.502926
                4  0.587249
                5  0.375806
                6 -0.140995
                7  0.002854
                8 -0.875326
                9  0.148876
                In [4]:
                
                df['uld'] = np.where(df['uld'] > 0, 1, 0)
                df
                Out[4]:
                   uld
                0    1
                1    0
                2    0
                3    0
                4    1
                5    1
                6    0
                7    1
                8    0
                9    1
                

                至于你做的失败的原因:

                As for why what you did failed:

                In [7]:
                
                if df['uld'] > 0:
                   df['uld'] = 1
                else:
                   df['uld'] = 0
                ---------------------------------------------------------------------------
                ValueError                                Traceback (most recent call last)
                <ipython-input-7-ec7d7aaa1c28> in <module>()
                ----> 1 if df['uld'] > 0:
                      2    df['uld'] = 1
                      3 else:
                      4    df['uld'] = 0
                
                C:WinPython-64bit-3.4.3.1python-3.4.3.amd64libsite-packagespandascoregeneric.py in __nonzero__(self)
                    696         raise ValueError("The truth value of a {0} is ambiguous. "
                    697                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
                --> 698                          .format(self.__class__.__name__))
                    699 
                    700     __bool__ = __nonzero__
                
                ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
                

                所以错误是您尝试使用 TrueFalse 评估数组,这变得模棱两可,因为有多个值要比较,因此错误.在这种情况下,你不能真正使用推荐的 anyall 等,因为你想屏蔽你的 df 并且只设置满足条件的值,那里在 pandas 网站上对此进行了解释:http://pandas.pydata.org/pandas-docs/dev/gotchas.html 和相关问题:ValueError:具有多个元素的数组的真值不明确.使用 a.any() 或 a.all()

                So the error is that you are trying to evaluate an array with True or False which becomes ambiguous because there are multiple values to compare hence the error. In this situation you can't really use the recommended any, all etc. as you are wanting to mask your df and only set the values where the condition is met, there is an explanation on the pandas site about this: http://pandas.pydata.org/pandas-docs/dev/gotchas.html and a related question here: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

                np.where 将布尔条件作为第一个参数,如果为真则返回第二个参数,否则返回第二个参数根据需要返回第三个参数.

                np.where takes a boolean condition as the first param, if that is true it'll return the second param, otherwise if false it returns the third param as you want.

                更新

                再次查看此内容后,您可以通过使用 astype 进行转换将布尔系列转换为 int:

                Having looked at this again you can convert the boolean Series to an int by casting using astype:

                In [23]:
                df['uld'] = (df['uld'] > 0).astype(int)
                df
                
                Out[23]:
                   uld
                0    1
                1    0
                2    0
                3    0
                4    1
                5    1
                6    0
                7    1
                8    0
                9    1
                

                这篇关于使用条件语句替换 pandas DataFrame 中的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 与否?)

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

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