Home > Software engineering >  get count of all positive values at each row - pandas
get count of all positive values at each row - pandas

Time:01-28

I have dataset with values above and below 0 and I want to calculate count of ve and -ve with sum and count of values above a threshold added as new column. This dataset has 60 columns.

Dataset

A  B       C  D  E
0  foo   1.2 -1  2
1  bar   1.3 -2 -4
2  baz   2.1  2  5

The desired result,

A  B         C  D  E    postive_count   negative_count sum_pos   sum_neg    above_2
0  foo     1.2 -1  2                2               1      3.2        -1          1
1  bar     1.3 -2 -4                1               2      1.3        -6          0
2  baz     2.1  2  5                3               3      9.1         0          3

I have tried [1] 2 but these add it for columns and not full row. Any ideas would be appreciated!

CodePudding user response:

You need to use axis to apply the operation row-wise: df['positive_count'] = df[df>0].count(axis=1)

  •  Tags:  
  • Related