title: Python - Pandas random sampling per group
body: <p>I have a dataFrame really similar to that, but with thousands of values :</p>
  
  <pre><code>import numpy as np
  import pandas as pd 
  
  # Setup fake data.
  np.random.seed([3, 1415])      
  df = pd.DataFrame({
      'Class': list('AAAAAAAAAABBBBBBBBBB'),
      'type': (['short']*5 + ['long']*5) *2,
      'image name': (['image01']*2  + ['image02']*2)*5,
      'Value2': np.random.random(20)})
  </code></pre>
  
  <p>I was able to find a way to do a random sampling of 2 values per images, per Class and per Type with the following code :</p>
  
  <pre><code>df2 = df.groupby(['type', 'Class', 'image name'])[['Value2']].apply(lambda s: s.sample(min(len(s),2)))
  </code></pre>
  
  <p>I got the following result :</p>
  
  <p><a href="https://i.stack.imgur.com/KJ4bQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KJ4bQ.png" alt="My table"></a></p>
  
  <p>I'm looking for a way to subset that table to be able to randomly choose a random image ('image name')  per type and per Class (and conserve the 2 values for the randomly selected image.</p>
  
  <p>Excel Example of my desired output :</p>
  
  <p><a href="https://i.stack.imgur.com/cTBmN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cTBmN.png" alt="Desired output"></a></p>