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

Advertisements

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.

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

Leave a ReplyCancel reply