title: Pandas get all rows of min and max values after groupby
body: <p>I have a dataframe like this:</p>
  
  <pre><code>df = pd.DataFrame({'A' : list('ababababba'),
                     'B' : [1, 1, 1, 2, 2, 1,1,2,1,1],
                     'C' : [2.0, 5., 8., 1., 2., 9.,2.0,4.0,5.0,3.0],
                     'D' : [10,20,30,10,20,30,20,40,50,10]})
  </code></pre>
  
  <p>Required:</p>
  
  <pre><code>   A  B    C   D
  0  a  1  2.0  10 # a1 min keep
  1  b  1  5.0  20 # b1 min
  2  a  1  8.0  30 # a1 max keep
  3  b  2  1.0  10 
  4  a  2  2.0  20
                    # b1 removed
                    # a1 remove
  7  b  2  4.0  40
  8  b  1  5.0  50 # b1 max keep
  9  a  1  3.0  10 # a1 min keep
  </code></pre>
  
  <p>Related links:
  <a href="https://stackoverflow.com/questions/40978059/min-and-max-row-from-pandas-groupby">Min and max row from pandas groupby</a>  </p>
  
  <p><a href="https://stackoverflow.com/questions/49781015/max-and-min-from-two-series-in-pandas-groupby">Max and min from two series in pandas groupby</a>  </p>
  
  <p><a href="https://stackoverflow.com/questions/25024797/max-and-min-date-in-pandas-groupby">Max and Min date in pandas groupby</a>  </p>
  
  <p><a href="https://stackoverflow.com/questions/55186262/pandas-groupby-and-then-select-a-row-by-value-of-column-min-max-for-example">pandas groupby and then select a row by value of column (min,max, for example)</a></p>