Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to count positive and negative numbers of a column after applying groupby in pandas

Tags:

python

pandas

have the following dataframe:

      token name   ltp    change
0   12345.0  abc   2.0       NaN
1   12345.0  abc   5.0  1.500000
2   12345.0  abc   3.0 -0.400000
3   12345.0  abc   9.0  2.000000
4   12345.0  abc   5.0 -0.444444
5   12345.0  abc  16.0  2.200000
6    6789.0  xyz   1.0       NaN
7    6789.0  xyz   5.0  4.000000
8    6789.0  xyz   3.0 -0.400000
9    6789.0  xyz  13.0  3.333333
10   6789.0  xyz   9.0 -0.307692
11   6789.0  xyz  20.0  1.222222

I need to count of positive and negative number for each category of the name column. in above example

abc:pos_count: 3 abc:neg_count:2
xyz:pos_count:2 xyz:neg_count:2

count=df.groupby('name')['change'].count()
count  

however, this gives me only the total count by group but not the positive & negative count separately.

like image 222
push kala Avatar asked Sep 17 '25 02:09

push kala


1 Answers

Use:

g = df.groupby('name')['change']
counts = g.agg(
    pos_count=lambda s: s.gt(0).sum(),
    neg_count=lambda s: s.lt(0).sum(),
    net_count=lambda s: s.gt(0).sum()- s.lt(0).sum()).astype(int)

Result:

# print(counts)
     pos_count  neg_count  net_count
name                                 
abc           3          2          1
xyz           3          2          1
like image 157
Shubham Sharma Avatar answered Sep 19 '25 15:09

Shubham Sharma