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

Compare values across multiple columns in pandas and count the instances in which values in the last column is higher than the others

I have a DataFrame that looks like this:

Image of DataFrame

What I would like to do is to compare the values in all four columns (A, B, C, and D) for every row and count the number of times in which D has the smaller value than A, B, or C for each row and add it into the ‘Count’ column. So for instance, ‘Count’ should be 1 for the second row, the third row, and 2 for the last row.

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

Thank you in advance!

>Solution :

In the future, please do not post data as an image.

Use a lambda function and compare across all columns, then sum across the columns.

data = {'A': [1,47,4316,8511],
        'B': [4,1,3,4],
        'C': [2,7,9,1],
        'D': [32,17,1,0]
       }

df = pd.DataFrame(data)

df['Count'] = df.apply(lambda x: x['D'] < x, axis=1).sum(axis=1)

Output:

      A  B  C   D  Count
0     1  4  2  32      0
1    47  1  7  17      1
2  4316  3  9   1      3
3  8511  4  1   0      3
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