How to do conditional count after group count on Pandas Dataframe?

I have the following data frame:

   key1  key2
0    a   one
1    a   two
2    b   one
3    b   two
4    a   one
5    c   two

Now I want to group the data frame by key1and count the column key2with the value "one"to get this result:

   key1  
0    a   2
1    b   1
2    c   0

I just get a regular bill with:

df.groupby(['key1']).size()

But I do not know how to insert a condition.

I tried things like this:

df.groupby(['key1']).apply(df[df['key2'] == 'one'])

But I can’t get further. How can i do this?

+19
source share
6 answers

I think you need to add a condition first:

#if need also category c with no values of 'one'
df11=df.groupby('key1')['key2'].apply(lambda x: (x=='one').sum()).reset_index(name='count')
print (df11)
  key1  count
0    a      2
1    b      1
2    c      0

Or use with , then the missing value is added : categoricalkey1size

df['key1'] = df['key1'].astype('category')
df1 = df[df['key2'] == 'one'].groupby(['key1']).size().reset_index(name='count') 
print (df1)
  key1  count
0    a      2
1    b      1
2    c      0

If all combinations are needed:

df2 = df.groupby(['key1', 'key2']).size().reset_index(name='count') 
print (df2)
  key1 key2  count
0    a  one      2
1    a  two      1
2    b  one      1
3    b  two      1
4    c  two      1

df3 = df.groupby(['key1', 'key2']).size().unstack(fill_value=0)
print (df3)
key2  one  two
key1          
a       2    1
b       1    1
c       0    1
+18
source

"" groupby "key2" :   df.groupby('key1')['key2'].apply(lambda x: x[x == 'one'].count())

key1
a    2
b    1
c    0
Name: key2, dtype: int64
+6

1

df.set_index('key1').key2.eq('one').sum(level=0).astype(int).reset_index()

  key1  key2
0    a     2
1    b     1
2    c     0

2

df.key2.eq('one').groupby(df.key1).sum().astype(int).reset_index()

  key1  key2
0    a     2
1    b     1
2    c     0

3

f, u = df.key1.factorize()
pd.DataFrame(dict(key1=u, key2=np.bincount(f, df.key2.eq('one')).astype(int)))

  key1  key2
0    a     2
1    b     1
2    c     0

4

pd.crosstab(df.key1, df.key2.eq('one'))[True].rename('key2').reset_index()

  key1  key2
0    a     2
1    b     1
2    c     0

5

pd.get_dummies(df.key1).mul(
   df.key2.eq('one'), 0
).sum().rename_axis('key1').reset_index(name='key2')

  key1  key2
0    a     2
1    b     1
2    c     0
+2

groupby() unstack().

df = df.groupby(['key1', 'key2']).size().unstack()
+1

, , , key2 'one'.

df2 = df.assign(oneCount =
 lambda x: [1 if row.key2 == 'one' else 0 for index, row in x.iterrows()])

  key1 key2  oneCount
0    a  one         1
1    a  two         0
2    b  one         1
3    b  two         0
4    a  one         1
5    c  two         0

.

df3 = df2.groupby('key1').agg({"oneCount":sum}).reset_index()

 key1  oneCount
0    a         2
1    b         1
2    c         0
+1

2 ( ) :

, 'key2', : df.groupby('key1') ['key2']. apply ( x: x [x == 'one']. count())

0

Source: https://habr.com/ru/post/1683942/


All Articles