Search code examples
pythondataframeboolean-expression

I want to change a boolean variable based on other boolean variables for every observation in a Python data frame


A data summary looks like this from the data frame. This just shows a subset where multiple A, B and C variables are True. I only want one of them to be true. I created the Multiples variable to select the ones to change and a summary looks like this table.

     A        B      C     Multiples
ID              
197 True    True    False   True
215 True    True    False   True
225 True    False   True    True
234 True    True    False   True
265 True    True    False   True
321 False   True    True    True

Here is an example of what the improved data would look like. In every row only one of A, B or C is True and it's the rightmost one. I need to know how make that change in Python. Don't worry about the Multiples variable. When I re-compute that, with A, B and C as they are below, all the Multiples will be False. I already have the code to do that.

 A        B      C     Multiples

ID
197 False True False False
215 False True False False
225 False False True False
234 False True False False
265 False True False False
321 False False True False

I've searched the web and this site and can't find anything that works, at least that I understand.

Here is my current Python code:

for index, item in enumerate(df['A']):
    if ((df['Multiples'] == True) & (df['C'] == True | df['B'] == True)):
        df['Multiples'] = False

Solution

  • I managed to solve the problem myself with the following code:

    
    def clear_redundant(row):
        # no need to change rows with only one True value
        if not row['Multiples']:
            return
        # if the last column is true, both earlier should be false
        if row['C']:
            row['A'] = False
            row['B'] = False
        # if the middle column is true, the first should be false
        elif row['B']:
            row['A'] = False
    
    df.apply(clear_redundant, axis="columns")