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

Question:

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!

Asked By: palutuna

||

Answers:

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
Answered By: Stu Sztukowski

You can use vectorize the operation using gt and sum methods along an axis:

df['Count'] = df[['A', 'B', 'C']].gt(df['D'], axis=0).sum(axis=1)
print(df)

# Output
   A  B  C  D  Count
0  1  2  3  4      0
1  4  3  2  1      3
2  2  1  4  3      1
Answered By: Corralien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.