Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a dummy comparison variable in python and pandas

I have a dataframe df as follows:

C1  C2  C3
a1  a   d
b1  b   e
c1  c   f

If I do df[df['C1'] == 'a1'] I get

C1  C2  C3
a1  a   d

Is there some way of comparing things so that if I do df[df['C1'] == 'what should I use here'], I get all rows. i.e. I am looking for a dummy comparison variable so that comparison statement is ignored and all rows are returned.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

C1  C2  C3
a1  a   d
b1  b   e
c1  c   f

Edit: As the code is grandfathered, I need to use only == comparison statement. I can’t use !=. The comparison variable is coming through a for loop and I can insert a dummy variable in the for loop if available.

This is what the original code is which I can’t modify:

dfNew = []
for ii in ['a1', 'a2']:
 for jj in ['a', 'b']:
  dfNew.append(df[(df['C1'] == ii) & (df['C2'] == jj)])

What I am trying to achieve in dfNew is to also append values where if there was a dummy variable in ['a1', 'a2', 'dummy'] then only the other comparison operation is done. The number of columns is close to 50 so it will be impossible to code every pair to ignore in conditional operation.

Edit2:
How do you pass the dummy inside ['a1', 'a2', 'dummy'] ?

>Solution :

If you want a real Dummy variable, the official and cleanest way would be to create a class where __eq__ (equality check) always returns True:

class Dummy:
    def __eq__(self, other):
        return True

And now:

>>> df[df['C1'] == Dummy()]
   C1 C2 C3
0  a1  a  d
1  b1  b  e
2  c1  c  f
>>> 

Always gives True.

The Dummy class returns True for all equality checks.

For the edit.

Just do:

['a1', 'a2', Dummy()]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading