Pandas dataframe vectorized sampling
<p>I have a simple df forming a pivot_table:</p>

<pre><code>    d = {'one' : ['A', 'B', 'B', 'C', 'C', 'C'], 'two' : [6., 5., 4., 3., 2., 1.],     'three' : [6., 5., 4., 3., 2., 1.], 'four' : [6., 5., 4., 3., 2., 1.]}
    df = pd.DataFrame(d)
    pivot = pd.pivot_table(df,index=['one','two'])
</code></pre>

<p>I would like to randomly sample 1 row from each different element from column 'one' of the resulting pivot object.  (In this example, 'A' will always be sampled while there are more options for 'B' and 'C'.) I just began using the 0.18.0 version of pandas and am aware of the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sample.html?highlight=sample#pandas.DataFrame.sample" rel="nofollow">.sample</a> method.  I messed with the .groupby method applying a sampling function something like this:</p>

<pre><code>    grouped = pivot.groupby('one').apply(lambda x: x.sample(n=1, replace=False))
</code></pre>

<p>I raise a KeyError when I tried variations on that theme so I thought it was time for some fresh perspective on this seemingly simple question...</p>

<p>Thanks for any assistance!</p>