<legend id='81UUU'><style id='81UUU'><dir id='81UUU'><q id='81UUU'></q></dir></style></legend>

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

          <bdo id='81UUU'></bdo><ul id='81UUU'></ul>

        <small id='81UUU'></small><noframes id='81UUU'>

        如何迭代 pandas 数据框并创建新列

        How to iterate over pandas dataframe and create new column(如何迭代 pandas 数据框并创建新列)

          <bdo id='3U45o'></bdo><ul id='3U45o'></ul>
          <legend id='3U45o'><style id='3U45o'><dir id='3U45o'><q id='3U45o'></q></dir></style></legend>

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

                <small id='3U45o'></small><noframes id='3U45o'>

                  本文介绍了如何迭代 pandas 数据框并创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个有 2 列的 pandas 数据框.我想遍历它的行并基于第 2 列中的字符串我想在新创建的第 3 列中添加一个字符串.我试过了:

                  I have a pandas dataframe that has 2 columns. I want to loop through it's rows and based on a string from column 2 I would like to add a string in a newly created 3th column. I tried:

                  for i in df.index:
                      if df.ix[i]['Column2']==variable1:
                          df['Column3'] = variable2
                      elif df.ix[i]['Column2']==variable3:
                          df['Column3'] = variable4
                  
                  print(df)
                  

                  但生成的数据框在第 3 列中只有变量 2.

                  But the resulting dataframe has in column 3 only Variable2.

                  有什么想法我还能做到这一点吗?

                  Any ideas how else I could do this?

                  推荐答案

                  我认为你可以使用 double numpy.where,什么比循环更快:

                  I think you can use double numpy.where, what is faster as loop:

                  df['Column3'] = np.where(df['Column2']==variable1, variable2, 
                                  np.where(df['Column2']==variable3, variable4))
                  

                  如果两个条件都为False,则需要添加变量:

                  And if need add variable if both conditions are False:

                  df['Column3'] = np.where(df['Column2']==variable1, variable2, 
                                  np.where(df['Column2']==variable3, variable4, variable5))
                  

                  示例:

                  df = pd.DataFrame({'Column2':[1,2,4,3]})
                  print (df)
                     Column2
                  0        1
                  1        2
                  2        4
                  3        3
                  
                  variable1 = 1
                  variable2 = 2
                  variable3 = 3
                  variable4 = 4
                  variable5 = 5
                  
                  df['Column3'] = np.where(df['Column2']==variable1, variable2, 
                                  np.where(df['Column2']==variable3, variable4, variable5))
                  
                  print (df)
                     Column2  Column3
                  0        1        2
                  1        2        5
                  2        4        5
                  3        3        4
                  

                  另一个解决方案,谢谢<代码>乔恩克莱门茨:

                  Another solution, thanks Jon Clements:

                  df['Column4'] = df.Column2.map({variable1: variable2, variable3:variable4}).fillna(variable5)
                  print (df)
                     Column2  Column3  Column4
                  0        1        2      2.0
                  1        2        5      5.0
                  2        4        5      5.0
                  3        3        4      4.0
                  

                  这篇关于如何迭代 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 与否?)
                  <i id='DtF7a'><tr id='DtF7a'><dt id='DtF7a'><q id='DtF7a'><span id='DtF7a'><b id='DtF7a'><form id='DtF7a'><ins id='DtF7a'></ins><ul id='DtF7a'></ul><sub id='DtF7a'></sub></form><legend id='DtF7a'></legend><bdo id='DtF7a'><pre id='DtF7a'><center id='DtF7a'></center></pre></bdo></b><th id='DtF7a'></th></span></q></dt></tr></i><div id='DtF7a'><tfoot id='DtF7a'></tfoot><dl id='DtF7a'><fieldset id='DtF7a'></fieldset></dl></div>

                    <tbody id='DtF7a'></tbody>
                1. <small id='DtF7a'></small><noframes id='DtF7a'>

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

                          1. <tfoot id='DtF7a'></tfoot>
                          2. <legend id='DtF7a'><style id='DtF7a'><dir id='DtF7a'><q id='DtF7a'></q></dir></style></legend>