title: making and updating multiple pandas dataframes using dicts (avoiding repetative code)
body: <p>I have a dataframe of id numbers (n = 140, but it could be more or less) and I have 5 group leaders. Each group leader needs to be randomly assigned an  amount of these ids (for ease lets make it even so n=28, but I need to be able to control the amounts) and those rows need to be split out into a new <code>df</code> and then droped from the original dataframe so that there is no crossover between leaders.</p>
  <pre><code>import pandas as pd
  import numpy as np
  
  #making the df
  df = pd.DataFrame()
  df['ids'] = np.random.randint(1, 140, size=140)
  df['group_leader'] = ''
  
  
  # list of leader names
  leaders = ['John', 'Paul', 'George', 'Ringo', 'Apu']
  </code></pre>
  <p>I can do this for each leader with something like</p>
  <pre><code>df.loc[df.sample(n=28).index, 'group_leader'] = 'George'
  g = df[df['group_leader']=='George'].copy()
  df = df[df['group_leader] != 'George']
  print(df.shape()[0]) #double checking that df has less ids in it
  
  </code></pre>
  <p>However, doing this individually for each group leaders seems really un-pythonic (not that I'm an expert on that) and is not easy to refactor into a function.</p>
  <p>I thought that I might be able to do it with a <code>dict</code> and a <code>for loop</code></p>
  <pre><code>frames = dict.fromkeys('group_leaders', pd.DataFrame())
  
  for i in frames.keys(): #allows me to fill the cells with the string key?
      df.loc[df.sample(n=28).index, 'group_leader'] = str(i)
      frames[i].update(df[df['group_leader']== str(i)].copy())#also tried append()
      print(frames[i].head())
      df = df[df['group_leader'] != str(i)]
      print(f'df now has {df.shape[0]} ids left') #just in case there's a remainder of ids
  </code></pre>
  <p>However, the new dataframes are still empty and I get the error:</p>
  <pre><code>    Traceback (most recent call last):
    File &quot;C:\Users\path\to\the\file\file.py&quot;, line 38, in &lt;module&gt;
      df.loc[df.sample(n=28).index, 'group_leader'] = str(i)
    File &quot;C:\Users\path\to\the\file\pandas\core\generic.py&quot;, line 5356, in sample
      locs = rs.choice(axis_length, size=n, replace=replace, p=weights)
    File &quot;mtrand.pyx&quot;, line 909, in numpy.random.mtrand.RandomState.choice
  ValueError: a must be greater than 0 unless no samples are taken
  </code></pre>
  <p>This leads me to believe that I'm doing two things wrong:</p>
  <ol>
  <li>Either making the dict incorectly or updating it incorrectly.</li>
  <li>Making the for loop run in such a way that it tries to run 1 too many times.</li>
  </ol>
  <p>I have tried to be as clear as possible and present a minimally useful version of what I need, any help would be appreciated.</p>
  <p>Note - I'm aware that 5 divides well into 140 and there may be cases where this isn't the case but I'm pretty sure I can handle that myself with <code>if-else</code> if it's needed.</p>