【问题标题】:populate a pandas column with the id from the min value of another pandas DF使用来自另一个熊猫 DF 的最小值的 id 填充熊猫列
【发布时间】:2018-07-14 01:26:18
【问题描述】:

我希望遍历orders 的列表,并为每个订单分配一个所有者idid 在单独的pandas dataframe 中(我也尝试将其更改为SeriesOrderedDict。我想从df 中找到最小值并将其用于第一个@987654328 @ 在orders 中,然后将ids count 的计数加1,并重复直到所有订单都被执行。

可重现的示例:

df = pd.DataFrame({'Id':['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], 'count':[2, 3, 5, 6, 8, 9, 12, 13, 15, 55]})
orders = pd.DataFrame({'order_id':['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'a11', 'a12', 'a13']})
orders['newowner'] = ""

所有者:

df
  Id  count
0  a      2
1  b      3
2  c      5
3  d      6
4  e      8
5  f      9
6  g     12
7  h     13
8  i     15
9  j     55

订单:

   order_id newowner
0        a1         
1        a2         
2        a3         
3        a4         
4        a5         
5        a6         
6        a7         
7        a8         
8        a9         
9       a10         
10      a11         
11      a12         
12      a13         

预期结果:

   order_id newowner
0        a1       a    # brings a up to 3 records
1        a2       a    # a and b are tied with 3, so it goes to a again (doesn't matter which gets it first)
2        a3       b    # now b has 3, and a has 4, so it goes to b
3        a4       a    # both have 4 so a
4        a5       b    # etc.
5        a6       a
6        a7       b
7        a8       c
8        a9       a
9       a10       b
10      a11       c
11      a12       a
12      a13       b

我尝试找到df.count 的最小值,并尝试遍历每个,但很难隔离每个订单。

for order in orders.iteritems():
    order['newowner'] = df.count.min()

for order in orders.iteritems():
    for name in df.iteritems:
        idx = df[df.count == df.count.min()]['Id']
    order['newonwer'] = idx

【问题讨论】:

    标签: python pandas for-loop dataframe


    【解决方案1】:

    我不确定我会这样做。如果可能的话,我可能会寻找一种使用df.apply 的方法。但我认为这段代码会给你预期的结果。

    for idx, order in orders.iterrows():
        idxmin = df['count'].idxmin()
        df.loc[idxmin, 'count'] += 1
        order['newowner'] = df.loc[idxmin,'Id']
    

    【讨论】:

      【解决方案2】:

      这是df.apply的一种方式:

      def set_owner(order_id):
          min_idx = df['count'].idxmin()
          df.loc[min_idx, 'count'] += 1
          return df.loc[min_idx, 'Id']
      
      orders['newowner'] = orders['order_id'].apply(set_owner)
      
      orders
      #    order_id newowner
      # 0        a1        a
      # 1        a2        a
      # 2        a3        b
      # 3        a4        a
      # 4        a5        b
      # 5        a6        a
      # 6        a7        b
      # 7        a8        c
      # 8        a9        a
      # 9       a10        b
      # 10      a11        c
      # 11      a12        d
      # 12      a13        a
      
      df
      #   Id  count
      # 0  a      8
      # 1  b      7
      # 2  c      7
      # 3  d      7
      # 4  e      8
      # 5  f      9
      # 6  g     12
      # 7  h     13
      # 8  i     15
      # 9  j     55
      

      【讨论】:

      • 非常感谢,我会试试这个。快问:set_owner(row)中的row是什么
      • 当您使用 df.apply 时,通常您希望对行中的值做一些事情,例如您想将order_id 用作某些函数的一部分(通过row['order_id']set_owner 中调用它)。在您的特定用例中,这无关紧要,因为您的结果只是 df 表的函数。
      • 编辑为使用 pd.Series.apply,这可能会稍微快一些。
      猜你喜欢
      • 2021-03-18
      • 2020-11-26
      • 2019-06-05
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 2017-06-26
      相关资源
      最近更新 更多