Forward/Backward fill na by incrementing/decrementing last found value?

后端 未结 1 1552
余生分开走
余生分开走 2021-01-24 19:17

Given the following pandas data frame (a copy of it can by found here). How to fill na in a separate column with incrementing/decrementing nr of rows until the next sig

相关标签:
1条回答
  • 2021-01-24 19:53

    The usual approach to signal-based groupings (which we should really have better native support for, IMHO) to use the compare-cumsum-groupby pattern. Here the comparison is to determine whether a signal entry is null or not, after which we do a cumulative sum so that each signal group has its own id (group id, or gid). The rest is just arithmetic.

    While there's some duplication here we could refactor away, I'm feeling lazy, and so:

    gid = df["Signal"].notnull().cumsum()
    dg = df.groupby(gid)
    sign = dg["Signal"].transform("first")
    df["forward signal rows"] = (dg.cumcount() + 1) * sign
    df["backward signal rows"] = (dg["Signal"].transform("size") - dg.cumcount()) * sign
    df["value at signal"] = dg["Values"].transform("last")
    df.loc[gid == 0, "value at signal"] = np.nan
    

    gives me a frame matching your target one.

    0 讨论(0)
提交回复
热议问题