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

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

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

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

        Pandas:用于在 DataFrame 中设置值的三元条件运算符

        时间:2023-08-29
      2. <i id='RCy58'><tr id='RCy58'><dt id='RCy58'><q id='RCy58'><span id='RCy58'><b id='RCy58'><form id='RCy58'><ins id='RCy58'></ins><ul id='RCy58'></ul><sub id='RCy58'></sub></form><legend id='RCy58'></legend><bdo id='RCy58'><pre id='RCy58'><center id='RCy58'></center></pre></bdo></b><th id='RCy58'></th></span></q></dt></tr></i><div id='RCy58'><tfoot id='RCy58'></tfoot><dl id='RCy58'><fieldset id='RCy58'></fieldset></dl></div>
      3. <tfoot id='RCy58'></tfoot>

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

            <tbody id='RCy58'></tbody>

              <legend id='RCy58'><style id='RCy58'><dir id='RCy58'><q id='RCy58'></q></dir></style></legend>
              • <bdo id='RCy58'></bdo><ul id='RCy58'></ul>

                  本文介绍了Pandas:用于在 DataFrame 中设置值的三元条件运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个数据框 pd.我想更改列 irr 的值,具体取决于它是高于还是低于阈值.

                  I have a dataframe pd. I would like to change a value of column irr depending on whether it is above or below a thresh hold.

                  我怎样才能在一行中做到这一点?现在我有

                  How can I do this in a single line? Now I have

                  pd['irr'] = pd['irr'][pd['cs']*0.63 > pd['irr']] = 1.0
                  pd['irr'] = pd['irr'][pd['cs']*0.63 <=  pd['irr']] = 0.0
                  

                  问题当然是我改了irr,在下一行再次检查.

                  The problem of course is that I change irr and check it again in the next line.

                  是否有类似 pandas 的三元条件运算符?

                  Is there something like a ternary conditional operator for pandas?

                  推荐答案

                  在 pandas 中没有,在 numpy 中是.

                  In pandas no, in numpy yes.

                  您可以使用 numpy.where 或将条件创建的 boolean Series 转换为 float - True1.0>Falses 是 0.0:

                  You can use numpy.where or convert boolean Series created by condition to float - Trues are 1.0 and Falses are 0.0:

                  pd['irr'] = np.where(pd['cs']*0.63 > pd['irr'], 1.0, 0.0)
                  

                  或者:

                  pd['irr'] = (pd['cs']*0.63 > pd['irr']).astype(float)
                  

                  示例:

                  pd = pd.DataFrame({'cs':[1,2,5],
                                     'irr':[0,100,0.04]})
                  
                  print (pd)
                     cs     irr
                  0   1    0.00
                  1   2  100.00
                  2   5    0.04
                  
                  pd['irr'] = (pd['cs']*0.63 > pd['irr']).astype(float)
                  print (pd)
                     cs  irr
                  0   1  1.0
                  1   2  0.0
                  2   5  1.0
                  

                  这篇关于Pandas:用于在 DataFrame 中设置值的三元条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:raise 条件表达式上的语句 下一篇:如何评估 SymPy 在初始条件下给出的常数?

                  相关文章

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

                2. <legend id='xdQ5O'><style id='xdQ5O'><dir id='xdQ5O'><q id='xdQ5O'></q></dir></style></legend>

                      • <bdo id='xdQ5O'></bdo><ul id='xdQ5O'></ul>

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