Search code examples
pythonpython-3.xpandasdataframeflags

How to set flags as -1 for n-1 rows before a cell in python(to drop)?


How do I set the n-1 values before a flag value to -1 as I need to drop the n-1 values before the flag value for example, if the flag value is 4, I need to set the previous 3 rows to -1.

Example Table

UID exclusion_flag
1BOP2UC-1 0
1BOP2UC-2 0
1BOP2UC-3 0
1BOP2UC-4 4
1BOP2UC-5 0
1BOP2UC-6 0
1BOP2UC-7 0
1BOP2UC-8 2
1BOP2UC-9 0
1BOP2UD-1 0
1BOP2UD-2 0
1BOP2UD-3 0
1BOP2UD-4 0
1BOP2UD-5 4

Required Solution

UID exclusion_flag
1BOP2UC-1 -1
1BOP2UC-2 -1
1BOP2UC-3 -1
1BOP2UC-4 4
1BOP2UC-5 0
1BOP2UC-6 0
1BOP2UC-7 -1
1BOP2UC-8 2
1BOP2UC-9 0
1BOP2UD-1 0
1BOP2UD-2 -1
1BOP2UD-3 -1
1BOP2UD-4 -1
1BOP2UD-5 4

Solution

  • I don't know whether question is still actual, but this is my solution:

    1. I initialize mem variable wich represents counter.
    2. I iterate thru list backwards using reversed function.
    3. If actual value is greater than zero, I want to overwrite mem with actual value otherwise, mem -1. I don't want counter to be negative, therefore max(0, mem-1).
    4. If mem is greater then zero, it means we are still counting rows, mark row to -1.
    5. If mem equals to x this is row, where our counter sequence started, and we want to keep x besides out.
    6. I return the out_arr again in correct order.
    
    from typing import List
    import pandas as pd
    
    dummy_col = [0, 0, 0, 0, 4, 0, 0, 0, 2]
    df = pd.DataFrame(dict(col1=dummy_col))
    
    
    def fun(arr: List[int]) -> List[int]:
        mem = 0
        out_arr = []
        for x in reversed(arr):
            mem = x if x > 0 else max(0, mem - 1)
            out = -1 if mem > 0 else 0
            out = x if mem == x else out
            out_arr += [out]
        return list(reversed(out_arr))
    
    
    df['final_col'] = fun(arr=df['col1'].to_list())
    print(df)
    

    OUTPUT:

       col1  final_col
    0     0          0
    1     0         -1
    2     0         -1
    3     0         -1
    4     4          4
    5     0          0
    6     0          0
    7     0         -1
    8     2          2