Home > Mobile >  Compare values across multiple columns in pandas and count the instances in which values in the last
Compare values across multiple columns in pandas and count the instances in which values in the last

Time:01-12

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!

CodePudding user response:

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

CodePudding user response:

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
  • Related