简体   繁体   English

如何在 Pandas Dataframe 上的 groupby 之后进行条件计数?

[英]How to do a conditional count after groupby on a Pandas Dataframe?

I have the following dataframe:我有以下数据框:

   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 dataframe by the key1 and count the column key2 with the value "one" to get this result:现在,我想按key1对数据框进行分组,并使用值"one"计算列key2以获得此结果:

   key1  
0    a   2
1    b   1
2    c   0

I just get the usual count with:我只是得到通常的计数:

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

But I don't know how to insert the condition.但我不知道如何插入条件。

I tried things like this:我试过这样的事情:

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

But I can't get any further.但我不能再进一步了。 How can I do this?我该怎么做?

I think you need add 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 categorical with key1 , then missing value is added by size :或者使用categoricalkey1 ,然后按size添加缺失值:

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 need all combinations:如果需要所有组合:

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

You can count the occurence of 'one' for the groupby dataframe, in the column 'key2' like this: df.groupby('key1')['key2'].apply(lambda x: x[x == 'one'].count())您可以在“key2”列中为 groupby 数据df.groupby('key1')['key2'].apply(lambda x: x[x == 'one'].count())计算“one”的出现次数,如下所示: df.groupby('key1')['key2'].apply(lambda x: x[x == 'one'].count())

yield产量

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

Option 1选项 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

Option 2选项 2

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

  key1  key2
0    a     2
1    b     1
2    c     0

Option 3选项 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

Option 4选项 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

Option 5选项 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

您可以通过在两个键和 unstack() 上应用 groupby() 来做到这一点。

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

Maybe not the fastest solution, but you can create new data frame with column of ones if key2 is equal to 'one'.也许不是最快的解决方案,但如果 key2 等于“一”,您可以创建带有一列的新数据框。

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

And then aggregate it.然后聚合它。

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

 key1  oneCount
0    a         2
1    b         1
2    c         0

I need count 2 columns (lambda with two arguments) as the example:我需要计算 2 列(带有两个参数的 lambda)作为示例:

Pandas dataframe groupby func , in the column key2 like this: Pandas dataframe groupby func ,在key2列中,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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