title: Return subset/slice of Pandas dataframe based on matching column of other dataframe, for each element in column?
body: <p>So I <em>think</em> this is a relatively simple question:</p>
  
  <p>I have a Pandas data frame (A) that has a key column (which is not unique/will have repeats of the key)</p>
  
  <p>I have another Pandas data frame (B) that has a key column, which may have many matching entries/repeats.</p>
  
  <p>So what I'd like is a bunch of data frames (a list, or a bunch of slice parameters, etc.), one for each key in A (regardless of whether it's unique or not)</p>
  
  <p>In [bad] pseudocode:</p>
  
  <pre><code> for each key in A:
     resultDF[] = Rows in B where B.key = key
  </code></pre>
  
  <p>I can easily do this iteratively with loops, but I've read that you're supposed to slice/merge/join data frames holistically, so I'm trying to see if I can find a better way of doing this.</p>
  
  <p>A join will give me all the stuff that matches, but that's not exactly what I'm looking for, since I need a resulting dataframe for <em>each</em> key (i.e. for every row) in A.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT:</strong>
  I was trying to be brief, but here are some more details:</p>
  
  <p>Eventually, what I need to do is generate some simple statistical metrics for elements in the columns of each row.</p>
  
  <p>In other words, I have a DF, call it A, and it has a r rows, with c columns, one of which is a key.  There may be repeats on the key.</p>
  
  <p>I want to "match" that key with another [set of?] dataframe, returning however many rows match the key.  Then, for that set of rows, I want to, say, determine the min and max of certain element (and std. dev, variance, etc.) and then determine if the corresponding element in A falls within that range.  </p>
  
  <p>You're absolutely right that it's possible that if row 1 and row 3 of DF A have the same key -- but potentially DIFFERENT elements -- they'd be checked against the same result set (the ranges of which obviously won't change).  That's fine.  These won't likely ever be big enough to make that an issue (but if there's the better way of doing it, that's great).</p>
  
  <p>The point is that I need to be able to do the "in range" and stat summary computation for EACH key in A.</p>
  
  <p>Again, I can easily do all of this iteratively. But this seems like the sort of thing pandas could do well, and I'm just getting into using it.</p>
  
  <p>Thanks again!</p>
  
  <p><strong>FURTHER EDIT</strong></p>
  
  <p>The DF looks like this:</p>
  
  <pre><code>df = pd.DataFrame([[1,2,3,4,1,2,3,4], [28,15,13,11,12,23,21,15],['keyA','keyB','keyC','keyD', 'keyA','keyB','keyC','keyD']]).T
  df.columns = ['SEQ','VAL','KEY']
  
    SEQ VAL   KEY
  0  1   28  keyA
  1  2   15  keyB
  2  3   13  keyC
  3  4   11  keyD
  4  1   12  keyA
  5  2   23  keyB
  6  3   21  keyC
  7  4   15  keyD
  </code></pre>
  
  <p>Both DF's A and B are of this format. </p>
  
  <p>I can iterative get the resultant sets by:</p>
  
  <pre><code>loop_iter = len(A) / max(A['SEQ_NUM']) 
  for start in range(0, loop_iter): 
       matchA =  A.iloc[start::loop_iter, :]['KEY']
  </code></pre>
  
  <p>That's simple.  But I guess I'm wondering if I can do this "inline".  Also, if for some reason the numeric ordering breaks (i.e. the SEQ get out of order) this this won't work.  There seems to be no reason NOT to do it explicitly splitting on the keys, right?  So perhaps I have TWO questions:  1). How to split on keys, iteratively (i.e. accessing a DF one row at a time), and 2). How to match a DF and do summary statistics, etc., on a DF that matches on the key.</p>
  
  <p>So, once again:</p>
  
  <p>1).  Iterate through DF A, going one at a time, and grabbing a key.
  2).  Match the key to the SET (matchB) of keys in B that match
  3).  Do some stats on "values" of matchB, check to see if val.A is in range, etc.
  4).  Profit!</p>