<small id='2Yg0T'></small><noframes id='2Yg0T'>

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

    1. <tfoot id='2Yg0T'></tfoot>

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

        基于 if-else 条件和查找创建新的 pandas 数据框列

        create new pandas dataframe column based on if-else condition with a lookup(基于 if-else 条件和查找创建新的 pandas 数据框列)
      2. <i id='kMarH'><tr id='kMarH'><dt id='kMarH'><q id='kMarH'><span id='kMarH'><b id='kMarH'><form id='kMarH'><ins id='kMarH'></ins><ul id='kMarH'></ul><sub id='kMarH'></sub></form><legend id='kMarH'></legend><bdo id='kMarH'><pre id='kMarH'><center id='kMarH'></center></pre></bdo></b><th id='kMarH'></th></span></q></dt></tr></i><div id='kMarH'><tfoot id='kMarH'></tfoot><dl id='kMarH'><fieldset id='kMarH'></fieldset></dl></div>
            <tbody id='kMarH'></tbody>
          <legend id='kMarH'><style id='kMarH'><dir id='kMarH'><q id='kMarH'></q></dir></style></legend>
          <tfoot id='kMarH'></tfoot>

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

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

                  本文介绍了基于 if-else 条件和查找创建新的 pandas 数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 pandas 数据框,我需要根据 if-else 条件创建一个新列.这个问题已经在这里多次出现(例如,Creating基于 if-elif-else 条件的新列).

                  I have a pandas dataframe and I need to create a new column based on an if-else condition. This question already came up here multiple times (e.g., Creating a new column based on if-elif-else condition).

                  但是,我无法应用建议的解决方案,因为我还需要在列表中查找值以检查条件.我无法使用建议的解决方案执行此操作,因为我不确定如何在外部函数中访问我的查找列表.我的查找列表需要是全局的,我想避免这种情况.我觉得应该有更好的方法来做到这一点.

                  However, I cannot apply the proposed solution, since I also need to look up values in a list in order to check the condition. I cannot do this with the proposed solution, because I am not sure how I can access my lookup-list in the external function. My lookup-list would need to be global, which I want to avoid. I have the feeling there should be a better way to do this.

                  考虑以下数据框df:

                  letters
                  A
                  B
                  C
                  D
                  E
                  F
                  

                  我还有一个包含查找值的列表:

                  I also have a list which contains lookup values:

                  lookup = [C,D]
                  

                  现在,我想在我的数据框中创建一个新列,其中包含 1 如果相应的值包含在 lookup0 如果这些值不在 lookup 中.

                  Now, I want to create a new column in my dataframe which contains 1 if the respective value is contained in lookup and 0 if the values is not in lookup.

                  典型的方法是:

                  df.apply(helper, axis=1)
                  
                  def helper(row):
                    if(row['letters'].isin(lookup)):
                       row['result'] = 1
                    else:
                       row['result'] = 0
                  

                  但是,我不知道如何在 helper() 中访问 lookup 而不使其成为全局对象.

                  However, I do not know how I can access lookup in helper() without making it global.

                  结果应该是这样的:

                  letters    result
                  A          0
                  B          0
                  C          1
                  D          1
                  E          0
                  F          0
                  

                  推荐答案

                  虽然这个问题和问题很相似:如何在数据框的某些行的所有列上使用pandas应用函数

                  Although this question is very similar to the question: How to use pandas apply function on all columns of some rows of data frame

                  我认为这里值得展示几个方法,在一行中使用 np.where 和从 isinisin 将返回一个布尔系列,其中任何行都包含列表中的任何匹配项:

                  I think here it's worth showing a couple methods, on a single line using np.where with a boolean mask generated from isin, isin will return a boolean Series where any rows contain any matches in your list:

                  In [71]:
                  lookup = ['C','D']
                  df['result'] = np.where(df['letters'].isin(lookup), 1, 0)
                  df
                  
                  Out[71]:
                    letters  result
                  0       A       0
                  1       B       0
                  2       C       1
                  3       D       1
                  4       E       0
                  5       F       0
                  

                  这里使用 2 个 loc 语句并使用 ~ 反转掩码:

                  here using 2 loc statements and using ~ to invert the mask:

                  In [72]:
                  df.loc[df['letters'].isin(lookup),'result'] = 1
                  df.loc[~df['letters'].isin(lookup),'result'] = 0
                  df
                  
                  Out[72]:
                    letters  result
                  0       A       0
                  1       B       0
                  2       C       1
                  3       D       1
                  4       E       0
                  5       F       0
                  

                  这篇关于基于 if-else 条件和查找创建新的 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='cfIC9'><style id='cfIC9'><dir id='cfIC9'><q id='cfIC9'></q></dir></style></legend>
                • <tfoot id='cfIC9'></tfoot>
                    <tbody id='cfIC9'></tbody>
                    <bdo id='cfIC9'></bdo><ul id='cfIC9'></ul>

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

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