XOR operation filtering the values of my list in the DataFrame columns

0

I have multiple lists and a single dataframe, my goal is to filter my columns containing the same values from my list and create a XOR operation dynamically. When I find my XOR result equal to 0, the line of my result should be deleted.

l1 = ['macao', 'banana']
l2 = ['uva','pera']

df = pd.DataFrame(np.random.randint(low=0, high=2, size=(5, 5)),
                  columns=['uva', 'pera', 'maca', 'banana', 'melao'])

XORoperation.

ResultafterdeletingtherowswheretheXORoperationisequalto0.

    
asked by anonymous 15.12.2018 / 18:34

1 answer

1

To create the XOR column, you can do this:

df = pd.DataFrame(np.random.randint(low=0, high=2, size=(5, 5)),
              columns=['uva', 'pera', 'maca', 'banana', 'melao'])



df['XOR maca_banana'] = df['maca'] ^ df['banana']

The symbol '^' in the code above is a representation of the XOR function in Python.

To remove rows whose XOR columns have at least a value of 0, you can use the following code:

df.drop(df[ (df['XOR maca_banana'] == 0) | (df['XOR uva_pera'] == 0) ].index, inplace=True)

Here the '| 'represents the OR function.

If you have any questions, do not hesitate to ask. I hope I could have helped you =)

    
18.12.2018 / 18:12