Apply a function to pandas dataframe and get different sized ndarray output
<p>My goal is to get the indexes of the local max heights of a dataframe. These change between 3 and 5 per column.</p>

<p>I have tried using the apply function, but get either the error <code>Shape of passed values is (736, 4), indices imply (736, 480)</code> or <code>could not broadcast input array from shape (0) into shape (1)</code>.</p>

<p>My matrix is <code>480x736</code>. </p>

<p>Here is what I've written for the apply function:</p>

<pre><code>import numpy as np
import peakutils
df.apply(lambda x: peakutils.indexes(x, thres=0.02/max(x), min_dist=100))
</code></pre>

<p>Here is what I am able to get to work:</p>

<pre><code>indexes =[]
import numpy as np
import peakutils
for column in df:
    indexes.append(peakutils.indexes(df[column], thres=0.02/max(df[column]), min_dist=100))
</code></pre>

<p>Often the indexes are 4 in length, but occasionally I'll get 1 more or less:</p>

<pre><code>Out[32]: 
[array([ 12, 114, 217, 328, 433]),
 array([ 12, 116, 217, 325, 433]),
 array([ 64, 166, 283, 389]),
 array([105, 217, 326, 433]),
 array([105, 237, 390])]
</code></pre>

<p>My guess is that the problem with the output comes from my not knowing the shape of the resultant dataframe. The shape is indeterminable from the outset.</p>

<p><strong>How do I apply a function to a df where the output differs in size &amp; type?</strong></p>

<p>pandas is trying to do "something" with the arrays.  You can short circuit that "something" by wrapping your <code>lambda</code>'s return value in a <code>pd.Series</code></p>

<p>Try this:</p>

<pre><code>df.apply(lambda x: pd.Series(peakutils.indexes(x, thres=0.02/max(x), min_dist=100)))
</code></pre>

python python-3.x pandas scipy