简体   繁体   中英

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

Need sum for count True s 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)

您可以在透视之前过滤数据帧:

pd.pivot_table(df.loc[df['value']>0], index='id',values='value',aggfunc='count')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM