[Python] How to replace values in a pandas dataframe column based on a condition

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe that looks like this

    import pandas as pd

    all_df = pd.DataFrame(
    [
    ('A','Area1','AA','A B D E','A B','D E'),
    ('B','Area1','AA','A B D E','A B','D E'),
    ('C','Area2','BB','C','C','C'),
    ('E','Area1','CC','A B D E','A B','D E'),
    ('F','Area3','BB','F G','G','F')
    ],
    columns = ['Name','Area','Type','Group','AA members','CC members']
    )


    Name Area Type Group AA members CC members
    0 A Area1 AA A B D E A B D E
    1 B Area1 AA A B D E A B D E
    2 C Area2 BB C C C
    3 E Area1 CC A B D E A B D E
    4 F Area3 BB F G G F


    The last row (row 4) is incorrect. Anything that is type BB should only have itself (F) in Group, AA members, CC members. In other words, it should look like this:

    4 F Area3 BB F F F


    To do this I tried:


    1. Check when Type is BB and length of Group is 2:

      all_df.loc[all_df['Type']== 'BB']['Group'].str.split().str.len() == 2

    2. Iterate over every row and to find the cases like this


    3. Make a new df with all these rows and make the Group, AA members, CC members = Name


    4. Drop the rows where that happens in all_df.


    5. Merge 3 back in to all_df.

    Is there a better pandas way to do this?

    Continue reading...

Compartilhe esta Página