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

    1. <tfoot id='k5MUM'></tfoot>

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

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

          <bdo id='k5MUM'></bdo><ul id='k5MUM'></ul>
      1. 使用条件在 pandas 数据框中生成新列

        Using conditional to generate new column in pandas dataframe(使用条件在 pandas 数据框中生成新列)
            • <bdo id='GytiH'></bdo><ul id='GytiH'></ul>
                <tbody id='GytiH'></tbody>
              • <i id='GytiH'><tr id='GytiH'><dt id='GytiH'><q id='GytiH'><span id='GytiH'><b id='GytiH'><form id='GytiH'><ins id='GytiH'></ins><ul id='GytiH'></ul><sub id='GytiH'></sub></form><legend id='GytiH'></legend><bdo id='GytiH'><pre id='GytiH'><center id='GytiH'></center></pre></bdo></b><th id='GytiH'></th></span></q></dt></tr></i><div id='GytiH'><tfoot id='GytiH'></tfoot><dl id='GytiH'><fieldset id='GytiH'></fieldset></dl></div>
                1. <small id='GytiH'></small><noframes id='GytiH'>

                  <tfoot id='GytiH'></tfoot>

                  <legend id='GytiH'><style id='GytiH'><dir id='GytiH'><q id='GytiH'></q></dir></style></legend>
                  本文介绍了使用条件在 pandas 数据框中生成新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个看起来像这样的熊猫数据框:

                  I have a pandas dataframe that looks like this:

                     portion  used
                  0        1   1.0
                  1        2   0.3
                  2        3   0.0
                  3        4   0.8
                  

                  我想基于 used 列创建一个新列,以便 df 看起来像这样:

                  I'd like to create a new column based on the used column, so that the df looks like this:

                     portion  used    alert
                  0        1   1.0     Full
                  1        2   0.3  Partial
                  2        3   0.0    Empty
                  3        4   0.8  Partial
                  

                  • 根据
                  • 创建一个新的alert
                  • 如果used1.0alert应该是Full.
                  • 如果 used0.0,则 alert 应为 Empty.
                  • 否则,alert 应该是 Partial.
                    • Create a new alert column based on
                    • If used is 1.0, alert should be Full.
                    • If used is 0.0, alert should be Empty.
                    • Otherwise, alert should be Partial.
                    • 最好的方法是什么?

                      推荐答案

                      你可以定义一个函数来返回你的不同状态Full"、Partial"、Empty"等,然后使用 df.apply将函数应用于每一行.请注意,您必须传递关键字参数 axis=1 以确保它将函数应用于行.

                      You can define a function which returns your different states "Full", "Partial", "Empty", etc and then use df.apply to apply the function to each row. Note that you have to pass the keyword argument axis=1 to ensure that it applies the function to rows.

                      import pandas as pd
                      
                      def alert(row):
                        if row['used'] == 1.0:
                          return 'Full'
                        elif row['used'] == 0.0:
                          return 'Empty'
                        elif 0.0 < row['used'] < 1.0:
                          return 'Partial'
                        else:
                          return 'Undefined'
                      
                      df = pd.DataFrame(data={'portion':[1, 2, 3, 4], 'used':[1.0, 0.3, 0.0, 0.8]})
                      
                      df['alert'] = df.apply(alert, axis=1)
                      
                      #    portion  used    alert
                      # 0        1   1.0     Full
                      # 1        2   0.3  Partial
                      # 2        3   0.0    Empty
                      # 3        4   0.8  Partial
                      

                      这篇关于使用条件在 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 与否?)

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

                    <tbody id='aUtnM'></tbody>

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

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