Reputation: 166
My goal is to get the indexes of the local max heights of a dataframe. These change between 3 and 5 per column.
I have tried using the apply function, but get either the error Shape of passed values is (736, 4), indices imply (736, 480)
or could not broadcast input array from shape (0) into shape (1)
.
My matrix is 480x736
.
Here is what I've written for the apply function:
import numpy as np
import peakutils
df.apply(lambda x: peakutils.indexes(x, thres=0.02/max(x), min_dist=100))
Here is what I am able to get to work:
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))
Often the indexes are 4 in length, but occasionally I'll get 1 more or less:
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])]
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.
How do I apply a function to a df where the output differs in size & type?
Upvotes: 1
Views: 1044
Reputation: 294488
pandas is trying to do "something" with the arrays. You can short circuit that "something" by wrapping your lambda
's return value in a pd.Series
Try this:
df.apply(lambda x: pd.Series(peakutils.indexes(x, thres=0.02/max(x), min_dist=100)))
Upvotes: 2