Selecting top n elements from each group in pandas groupby
<p>I have a dataframe which looks roughly like this:</p>

<pre><code>&gt;&gt;&gt; data
    price currency    
id                
2    1050       EU
5    1400       EU
4    1750       EU
8    4000       EU
7     630      GBP
1    1000      GBP
9    1400      GBP
3    2000      USD
6    7000      USD 
</code></pre>

<p>I need to get a new dataframe with <code>n</code> top-priced products for each currency, where <code>n</code> depends on currency and is given in another dataframe:</p>

<pre><code>&gt;&gt;&gt; select_number
          number_to_select
currency       
GBP         2
EU          2
USD         1
</code></pre>

<p>If I had to select the same number of top-priced elements, I could group the data by currency with <code>pandas.groupby</code> and then use <code>head</code> method of a grouped object.</p>

<p>However, <code>head</code> accepts only a number, not an array or some expression.</p>

<p>Of course, I can write a <code>for</code> loop, but this would we very awkward and inefficient way to do it.</p>

<p>How can do this in a good way?</p>