考虑到这个问题: Pandas conditional groupby count
我希望结果是这样的:
A D Dcount
0 foo 2 2
1 foo 4 2
2 foo 4 2
3 foo 2 2
4 bar 5 NaN
5 bar 4 NaN
6 bar 3 NaN
7 bar 2 NaN
我的意思是,如果满足2个条件(A列=' foo' B列= 2), 我希望在Dcount列中为A =' foo'的所有行设置此类行(2)的明显计数。
可以修改它以获得所需的结果吗?
import pandas as pd
df = pd.DataFrame(
{'A' : ['foo', 'foo', 'foo', 'foo',
'bar', 'bar', 'bar', 'bar'],
'D' : [2, 4, 4, 2, 5, 4, 3, 2]})
#First, I filter
df2=df.loc[(df['A']=='foo')&(df['D']==2)]
#Then, I use groupby and lambda x to count
df['Dcount']=df2.groupby(['D'])['D'].transform(lambda x: x.count())
df
提前致谢!
答案 0 :(得分:1)
您可以在一行中使用where
中的numpy
:
import numpy as np
df['Dcount'] = np.where(df['A']=='foo', sum((df.A=='foo') & (df.D==2)), np.NaN)
#In [34]: df
#Out[34]:
# A D Dcount
#0 foo 2 2
#1 foo 4 2
#2 foo 4 2
#3 foo 2 2
#4 bar 5 NaN
#5 bar 4 NaN
#6 bar 3 NaN
#7 bar 2 NaN