Reputation: 2164
Consider the following dataframe
d = {'N': [1, 2, 1, 2], 'NV': [50, 50,200,200], 'VAL' : [100,0,10,0]}
df = pd.DataFrame(data = d)
giving
N NV VAL
0 1 50 100
1 2 50 0
2 1 200 10
3 2 200 0
I would like to groupby in order to obtain, for each NV, the number of VAL entries which are strictly positive. The result I wish to obtain is the following
NV VAL
0 50 1
1 200 1
Any suggestions?
Upvotes: 1
Views: 40
Reputation: 21
try this
df['VAL'] = df.loc[df['VAL']>0].groupby(df['NV']).transform('count')
df.dropna(axis=0,inplace=True)
Upvotes: 0
Reputation: 4215
Try:
df[df.VAL>0].groupby('NV').size().reset_index().rename(columns={0:'VAL'})
NV VAL
0 50 1
1 200 1
Upvotes: 0
Reputation: 808
df.query("VAL > 0").groupby("NV").size()
or
df.groupby("NV").VAL.agg(lambda y: (y > 0).sum())
Upvotes: 3