XOR operation by filtering my list values in Dataframe columns

Asked

Viewed 60 times

0

I have several lists and a single dataframe, my goal is to filter my columns that contain the same values from my list and create a dynamic XOR operation. When you find my XOR result equal to 0, the row of this 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'])

inserir a descrição da imagem aqui

Operation XOR.

inserir a descrição da imagem aqui

Result after deleting lines where XOR operation equals 0.

inserir a descrição da imagem aqui

1 answer

1

To create the XOR column, you can do so:

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 above code 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 symbol ' | ' represents the OR function.

If you have any questions, don’t hesitate to ask. I hope I could have helped you =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.