[Python] Pandas groupby make all elements 0 if first element is 1

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following df:

    | day | first mover |
    | -------- | -------------- |
    | 1 | 1 |
    | 2 | 1 |
    | 3 | 0 |
    | 4 | 0 |
    | 5 | 0 |
    | 6 | 1 |
    | 7 | 0 |
    | 8 | 1 |


    i want to group this Data frame in the order bottom to top with a frequency of 4 rows. Furthermore if first row of group is 1 make all other entries 0. Desired output:

    | day | first mover |
    | -------- | -------------- |
    | 1 | 1 |
    | 2 | 0 |
    | 3 | 0 |
    | 4 | 0 |
    | 5 | 0 |
    | 6 | 0 |
    | 7 | 0 |
    | 8 | 0 |


    The first half i have accomplished. I am confuse about how to make other entries 0 if first entry in each group is 1.

    N=4
    (df.iloc[::-1].groupby(np.arange(len(df))//N

    Continue reading...

Compartilhe esta Página