Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Pivot Table Conditional Counting

I have a simple dataframe:

df = pd.DataFrame({'id': ['a','a','a','b','b'],'value':[0,15,20,30,0]})
df
  id  value
0  a      0
1  a     15
2  a     20
3  b     30
4  b      0

And I want a pivot table with the number of values greater than zero.

I tried this:

raw = pd.pivot_table(df, index='id',values='value',aggfunc=lambda x:len(x>0))

But returned this:

    value
id
a       3
b       2

What I need:

    value
id
a       2
b       1

I read lots of solutions with groupby and filter. Is it possible to achieve this only with pivot_table command? If it is not, which is the best approach?

Thanks in advance

UPDATE

Just to make it clearer why I am avoinding filter solution. In my real and complex df, I have other columns, like this:

df = pd.DataFrame({'id': ['a','a','a','b','b'],'value':[0,15,20,30,0],'other':[2,3,4,5,6]})
df
  id  other  value
0  a      2      0
1  a      3     15
2  a      4     20
3  b      5     30
4  b      6      0

I need to sum the column 'other', but when i filter I got this:

df=df[df['value']>0]
raw = pd.pivot_table(df, index='id',values=['value','other'],aggfunc={'value':len,'other':sum})
    other  value
id
a       7      2
b       5      1

Instead of:

    other  value
id
a       9      2
b      11      1
like image 591
O Pardal Avatar asked Mar 06 '23 16:03

O Pardal


1 Answers

Need sum for count Trues created by condition x>0:

raw = pd.pivot_table(df, index='id',values='value',aggfunc=lambda x:(x>0).sum())
print (raw)
    value
id       
a       2
b       1

As @Wen mentioned, another solution is:

df = df[df['value'] > 0]
raw = pd.pivot_table(df, index='id',values='value',aggfunc=len)
like image 99
jezrael Avatar answered Mar 20 '23 22:03

jezrael