更好/更优雅的方法来检查第一个条目

问题描述 投票:0回答:1

我对代码的优化有疑问。

Signal = pd.Series([1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0])

我有一个包含周期性位码的熊猫系列。我的目的是删除所有在特定序列之前开始的条目,例如1,1,0,0。因此在我的示例中,我期望像这样的简化序列:

[1, 1, 0, 0, 1, 1, 0, 0]

我已经有了1、1的解决方案,但是它不是很好,在我的示例中,修改也不容易:1,1,0,0。

    i = 0
    bool = True
    while bool:
        a = Signal.iloc[i]
        b = Signal.iloc[i + 1]
        if a == 1 and b == 1:
            bool = False
        else:
            i = i + 1

   Signal = Signal[i:]

感谢您的帮助。

python pandas series
1个回答
1
投票

我们可以使用view_as_windows来滚动查看该系列的窗口视图,检查该序列是否相等,并使用view_as_windows来找到第一个匹配项:

argmax
© www.soinside.com 2019 - 2024. All rights reserved.