Pandas DataFrame, smart apply of a complex function to groupby result
<p>I have a <code>pandas.DataFrame</code> with 3 columns of type <code>str</code> and <code>n</code> other columns of type <code>float64</code>.</p>

<p>I need to group rows by one of the three <code>str</code> columns and apply a function <code>myComplexFunc()</code> which will reduce `̀N  rows to one row.</p>

<p><code>myComplexFunc()</code> take only rows of type <code>float64</code>.</p>

<p>This can be done with some for loops but it will not be efficient, So I tried to use the <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html#flexible-apply" rel="nofollow">flexible apply</a> of <code>pandas</code> but it seems that it runs the heavy code of <code>myComplexFunc()</code> twice!</p>

<p>To be more clear, here is a minimal example</p>

<p><strong>Let "df" be a dataFrame like this</strong> :
</p>

<pre><code>df
&gt;&gt;
     A      B         C         D
0  foo    one  0.406157  0.735223
1  bar    one  1.020493 -1.167256
2  foo    two -0.314192 -0.883087
3  bar  three  0.271705 -0.215049
4  foo    two  0.535290  0.185872
5  bar    two  0.178926 -0.459890
6  foo    one -1.939673 -0.523396
7  foo  three -2.125591 -0.689809
</code></pre>

<p><strong>myComplexFunc()</strong></p>

<pre class="lang-py prettyprint-override"><code>def myComplexFunc(rows):
  # Some transformations that will return 1 row
  result = some_transformations(rows)
  return result
</code></pre>

<p><strong>What I want :</strong></p>

<pre class="lang-py prettyprint-override"><code># wanted apply is the name of the wanted method
df.groupby("A").wanted_apply(myComplexFunc)

&gt;&gt; 
    A    C            D
0  foo   new_c0_foo   new_d0_foo
1  bar   new_c0_bar   new_d0_bar
</code></pre>

<p>The column <code>B</code> have been removed because it's not of type <code>float64</code>. </p>

<p>Thanks in advance</p>