• <tfoot id='sNKPS'></tfoot>

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

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

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

        pandas 分组并使用最小值更新

        pandas groupby and update with min value( pandas 分组并使用最小值更新)
        • <bdo id='AiazH'></bdo><ul id='AiazH'></ul>
          <legend id='AiazH'><style id='AiazH'><dir id='AiazH'><q id='AiazH'></q></dir></style></legend>
          • <tfoot id='AiazH'></tfoot>

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

                  <i id='AiazH'><tr id='AiazH'><dt id='AiazH'><q id='AiazH'><span id='AiazH'><b id='AiazH'><form id='AiazH'><ins id='AiazH'></ins><ul id='AiazH'></ul><sub id='AiazH'></sub></form><legend id='AiazH'></legend><bdo id='AiazH'><pre id='AiazH'><center id='AiazH'></center></pre></bdo></b><th id='AiazH'></th></span></q></dt></tr></i><div id='AiazH'><tfoot id='AiazH'></tfoot><dl id='AiazH'><fieldset id='AiazH'></fieldset></dl></div>
                    <tbody id='AiazH'></tbody>
                  本文介绍了 pandas 分组并使用最小值更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的数据帧:

                  dfd = pd.DataFrame({'A': ['Apple','Apple', 'Apple','Orange','Orange','Orange','Pears','Pears'],
                                      'B': [1,2,9,6,4,3,2,1]
                                     })
                         A    B
                  0   Apple   1
                  1   Apple   2
                  2   Apple   9
                  3   Orange  6
                  4   Orange  4
                  5   Orange  3
                  6   Pears   2
                  7   Pears   1
                  

                  预期:

                     A    new_B   old_B
                  0   Apple   1   1
                  1   Apple   1   2
                  2   Apple   1   9
                  3   Orange  3   6
                  4   Orange  3   4
                  5   Orange  3   3
                  6   Pears   1   2
                  7   Pears   1   1
                  
                  预期的数据帧:new_values包含该组的最小值,对于Apple,最小列B的值是1,因此Apple的所有新值都是1,类似地,列B的Orange最小值是3,它在new_b列中被替换。

                  第二个预期产量: 一旦达到上述预期输出,我就必须为每个组创建SQL语句并写入文件: 基本上,迭代每一行并编写SQL查询:

                  sql_query= "update test_tbl "
                      "set id =  {0}"
                      "where id = {1}"
                      "and A = '{2}' ".format(new_b,old_b,A)
                  
                  print(sql_query, file=open("output.sql", "a"))
                  

                  推荐答案

                  使用GroupBy.transformFORSeries,大小与原始df相同:

                  dfd['new_B'] = dfd.groupby('A')['B'].transform('min')
                  print (dfd)
                          A  B  new_B
                  0   Apple  1      1
                  1   Apple  2      1
                  2   Apple  9      1
                  3  Orange  6      3
                  4  Orange  4      3
                  5  Orange  3      3
                  6   Pears  2      1
                  7   Pears  1      1
                  

                  如果列的顺序很重要,请使用insertrename

                  dfd.insert(1, 'new_B', dfd.groupby('A')['B'].transform('min'))
                  dfd = dfd.rename(columns={'B':'old_B'})
                  print (dfd)
                          A  new_B  old_B
                  0   Apple      1      1
                  1   Apple      1      2
                  2   Apple      1      9
                  3  Orange      3      6
                  4  Orange      3      4
                  5  Orange      3      3
                  6   Pears      1      2
                  7   Pears      1      1
                  

                  如果transform不可能,请使用以下替代解决方案:

                  #aggregate by min
                  s = dfd.groupby('A')['B'].min()
                  print (s)
                  A
                  Apple     1
                  Orange    3
                  Pears     1
                  Name: B, dtype: int64
                  
                  #insert and map
                  dfd.insert(1, 'new_B', dfd['A'].map(s))
                  dfd = dfd.rename(columns={'B':'old_B'})
                  print (dfd)
                          A  new_B  old_B
                  0   Apple      1      1
                  1   Apple      1      2
                  2   Apple      1      9
                  3  Orange      3      6
                  4  Orange      3      4
                  5  Orange      3      3
                  6   Pears      1      2
                  7   Pears      1      1
                  

                  这篇关于 pandas 分组并使用最小值更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)

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

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

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