[Python] Duplicate row in Pandas dataframe based on condition, then update a new column based...

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    I have a dataframe that looks like :

    [​IMG]

    df = pd.DataFrame({'qty': [10,7,2,1],
    'status 1': [5,2,2,0],
    'status 2': [3,2,0,1],
    'status 3': [2,3,0,0]
    })


    Each row has a qty of items. These items have one status (1,2 or 3).

    So qty = sum of values of status 1,2,3.

    I would like to :


    1. Duplicate each row by the "qty" column


    2. Then edit 3 status (or update a new column), to get just 1 status.

    The output should look like this:

    [​IMG]

    Edit: the order is not important, but I will need to keep other columns of my initial df.

    My (incomplete) solution so far - I found a way to duplicate the rows using this :

    df2= df2.loc[df2.index.repeat(df2['qty'])].reset_index(drop=True)


    But I can't find a way to fill the status.

    Do I need to use a for loop approach to fill the status?

    Should I do this transform in 1 or 2 steps?

    Something like: for each initial row, the n first rows take the first status, where n is the value of status 2....

    The output could maybe looks like :

    [​IMG]

    Edit1 : Thank you for your answers !

    Last question : now I'm trying to integrate this to my actual df. What is the best approach to apply these methods to my df who contains many other column ?

    I will answer my last question :


    1. Split df in 2: dfstatus and dfwithoutstatus, keeping the qty column in both


    2. Apply one of your method on the dfstatus


    3. Apply my method on the dfwithoutstatus (a simple duplication)


    4. Merge on index

    Thank you all for your answers.

    Best

    Continue reading...

Compartilhe esta Página