获取索引范围中行的最大值

2024-10-03 04:29:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我面临以下问题:我需要将Matlab代码重写为Pandas。你知道吗

问题如下: 我有高差数据。基于一个滚动窗口,我确定了移动平均值和高度差的标准差。当一行的高度差数据大于移动平均值+2*std时,它将被视为“峰值”(我需要确定)。原因是,峰值可以识别数据集中没有给出的悬置点。到目前为止,还不错。你知道吗

现在是我无法解决的更难的部分:彼此之间可能有多个峰。当一个峰值在另一个峰值的10个指数范围内(1个指数/行=0.25米,因此当一个峰值在2.5米范围内)时,则需要“合并”峰值:只需要保留高度差异最大的峰值。如果10个指数内的峰值没有被另一个峰值包围,则仅该值保持为最高安装点。你知道吗

另一个解决方案是将最大的高度差和指数分配给周围的山峰。你知道吗

我用滚动窗口的idxmax()尝试了一些东西,但没有成功。然后我尝试了下面的方法,但仍然无法理解。你知道吗

首先,我尝试将索引转换为列。 然后我过滤了heightdiff\u peak==True的数据帧 然后我计算了与下一个指数的差值。 并尝试获取到当前行中差值小于10的行的最大值。然而,这并不能给出正确的解决方案。你知道吗

数据帧如下所示:

df:
    Location    abs_diff_height heightdiff_peak index   difference_next_index
277 9.00    4.000000    True    277 1.0
278 9.25    5.000000    True    278 74.0
352 27.75   6.900000    True    352 39.0
391 37.50   6.000000    True    391 169.0
560 79.75   6.000000    True    560 1.0
561 80.00   5.900000    True    561 1.0
562 80.25   5.900000    True    562 1.0
563 80.50   8.900000    True    563 1.0
564 80.75   9.900000    True    564 1.0
565 81.00   10.900000   True    565 1.0
566 81.25   13.900000   True    566 1.0

我尝试了下面的代码,但不起作用。你知道吗

def get_max_value(df):
    return df.assign(
    max_diff_height = lambda df: np.where(df['difference_next_index']<10,
                                          df['abs_diff_height'].rolling(2).max().shift(1),
                                          df['abs_diff_height'])
    )


我也试过这样的方法:

df[['highest_peak']].rolling(20, center=True).apply(lambda s: s.idxmax(), raw=False)

然而,这只会导致NAN。你知道吗

matlab代码是:

%% Snap multiple detections in a row to the highest point of that peak.
% Initialise variables based on first detection value
x=2;
Remember=PeakIndexT(1);                                          
PeakIndex=PeakIndexT(1);
PeakValue=Dataset(PeakIndexT(1));
while x<=length(PeakIndexT)
    if PeakIndexT(x)-Remember>10                        % If there is more then 10 points (2.5 meters) difference between this and previous detection identify this one as a new one
        PeakIndex=[PeakIndex,PeakIndexT(x)];
        PeakValue=[PeakValue,Dataset(PeakIndexT(x))];

    else                                                % Else merge the detections and use the highest absolute value as the detection peak
        if PeakValue(end)<Dataset(PeakIndexT(x))
            PeakValue(end)=Dataset(PeakIndexT(x));
            PeakIndex(end)=PeakIndexT(x);
        end
    end
    Remember=PeakIndexT(x);                             % Store previous value for reference in loop
    x=x+1;
end


我期望的结果是最大值和索引。你知道吗

df:
    Location    abs_diff_height heightdiff_peak index   difference_next_index  max_value  index_max_value
277 9.00    4.000000    True    277 1.0     5.0 278 
278 9.25    5.000000    True    278 74.0    5.0 278
352 27.75   6.900000    True    352 39.0    6.9     352
391 37.50   6.000000    True    391 169.0   6.0     591
560 79.75   6.000000    True    560 1.0     13.9    566
561 80.00   5.900000    True    561 1.0     13.9    566
562 80.25   5.900000    True    562 1.0     13.9    566
563 80.50   8.900000    True    563 1.0     13.9    566
564 80.75   9.900000    True    564 1.0     13.9    566
565 81.00   10.900000   True    565 1.0     13.9    566
566 81.25   13.900000   True    566 1.0     13.9    566


Tags: 数据truedfindex高度valuediff指数
1条回答
网友
1楼 · 发布于 2024-10-03 04:29:07

IIUC,你首先需要groupby

s = df.difference_next_index.shift().gt(10)
df['index_max_value'] = (df.abs_diff_height                          
                           .groupby([s,s.cumsum()])
                           .transform('idxmax')
                         )

提供:

277    278.0
278    278.0
352    352.0
391    391.0
560    566.0
561    566.0
562    566.0
563    566.0
564    566.0
565    566.0
566    566.0
Name: abs_diff_height, dtype: float64

获取价值观很简单

df['max_value'] = df.loc[df['index_max_value'],'abs_diff_height']

相关问题 更多 >