Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Count values across rows that are greater than another value in a different column

I have a pandas dataframe like this:

        X   a   b   c
        1   1   0   2
        5   4   7   3
        6   7   8   9

I want to print a column called 'count' which outputs the number of values greater than the value in the first column('x' in my case). The output should look like:

X   a   b   c   Count
1   1   0   2   2
5   4   7   3   1
6   7   8   9   3

I would like to refrain from using 'lambda function' or 'for' loop or any kind of looping techniques since my dataframe has a large number of rows. I tried something like this but i couldn't get what i wanted.

df['count']=df [ df.iloc [:,1:] > df.iloc [:,0] ].count(axis=1)

I Also tried

numpy.where()

Didn't have any luck with that either. So any help will be appreciated. I also have nan as part of my dataframe. so i would like to ignore that when i count the values.

Thanks for your help in advance!

like image 575
sandy Avatar asked Oct 16 '25 14:10

sandy


1 Answers

You can using ge(>=) with sum

df.iloc[:,1:].ge(df.iloc[:,0],axis = 0).sum(axis = 1)
Out[784]: 
0    2
1    1
2    3
dtype: int64

After assign it back

df['Count']=df.iloc[:,1:].ge(df.iloc [:,0],axis=0).sum(axis=1)
df
Out[786]: 
   X  a  b  c  Count
0  1  1  0  2      2
1  5  4  7  3      1
2  6  7  8  9      3
like image 91
BENY Avatar answered Oct 18 '25 11:10

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!