title: Python Pandas Select values from another dataFrame depending on the total from another column
body: <p>I have a DataFrame below but I need to select rows from each code depending on the canceled and order column. </p>
  
  <p>Say code xxx has orders [6, 1, 5, 1] and the order is 11. I need an algorithm that can select the rows that meet the total 11 say rows with orders [6 &amp; 5]</p>
  
  <p>If No rows match, then select the closest ids and add it to a list with their difference from the canceled as below 111111 is the selected id and 35 is the diff between 55 and 20. i need an algorithm that can handle 10k Rows </p>
  
  <p>My Expected output</p>
  
  <pre><code>**code**    **canceled**    **order**              **ids**
       xxx           11.0            13     [128281, 128283]
       cvd             20            55         (111111, 35)
  </code></pre>
  
  <pre><code>import pandas as pd
  
  ccc = [
      {"code":"xxx","canceled":11.0,"id":"128281","order":6},
      {"code":"xxx","canceled":11.0,"id":"128282","order":1},
      {"code":"xxx","canceled":11.0,"id":"128283","order":5},
      {"code":"xxx","canceled":11.0,"id":"128284","order":1},
      {"code":"xxS","canceled":0.0,"id":"108664","order":4},
      {"code":"xxS","canceled":0.0,"id":"110515","order":1},
      {"code":"xxS","canceled":0.0,"id":"113556","order":1},
      {"code":"eeS","canceled":5.0,"id":"115236","order":1},
      {"code":"eeS","canceled":5.0,"id":"108586","order":1},
      {"code":"eeS","canceled":5.0,"id":"114107","order":1},
      {"code":"eeS","canceled":5.0,"id":"113472","order":3},
      {"code":"eeS","canceled":5.0,"id":"114109","order":3},
      {"code":"544W","canceled":44.0,"id":"107650","order":20},
      {"code":"544W","canceled":44.0,"id":"127763","order":4},
      {"code":"544W","canceled":44.0,"id":"128014","order":20},
      {"code":"544W","canceled":44.0,"id":"132434","order":58},
      {"code":"cvd","canceled":20.0,"id":"11111","order":55},
      {"code":"eeS","canceled":5.0,"id":"11111","order":5}
  ]
  </code></pre>
  
  <p>I tried a solution the below solution it works but i need to look for the exact value if it exists. I also need to select the most possible ids that sum up to the canceled value. I want to eliminate the possibility of having this <code>(111111, 35)</code></p>
  
  <pre><code>df = pd.DataFrame(ccc)
  
  def selected_ids(datum):
      ids = datum.id
      nbc = int(datum.canceled)
      order = datum.order
      count = []
      arr = []
  
      for loc, i in enumerate(order):
  
          count.append(i)
          arr.append(ids[loc])
  
          if nbc == int(i):
              return ids[loc]
  
          elif nbc  == 0:
              return ''
  
          elif nbc  &lt; int(i):
              return (ids[loc], (int(i)-nbc))
  
          if nbc &lt; sum(count):
               return [arr[:-1], (arr[-1],sum(count)-nbc)]
  
  xcv = df.sort_values('order').groupby('code').agg({
      'code':'first',
      'canceled': 'first',
      'order': list,
      'id':list
  })
  xcv['Orders_to_cancel'] = xcv.apply(
      selected_ids, axis = 1
  )
  xcv
  </code></pre>