[英]Count If with Condition Pandas DataFrame
I have a data frame:我有一个数据框:
A B C
0 xx No 1
1 xx No 2
2 xx Yes 3
3 xx Yes 4
4 xx No 1
5 xx No 2
6 xx No 3
7 xx Yes 4
8 xx No 1
9 xx No 2
If want to create a column that counts the number of times a number appear in column C, when column B is equal to Yes如果要创建一个列来统计某个数字在 C 列中出现的次数,当 B 列等于 Yes
so the output would be所以输出将是
A B C countifs
0 xx No 1
1 xx No 2
2 xx Yes 3 1
3 xx Yes 4 2
4 xx No 1
5 xx No 2
6 xx No 3
7 xx Yes 4 2
8 xx No 1
9 xx No 2
Is this possible?这可能吗?
For count only True
s values use GroupBy.transform
with sum
only for filtered rows by mask:对于仅计数
True
s值,使用GroupBy.transform
和sum
仅用于按掩码过滤的行:
m = df['B'].eq('Yes')
df['countifs'] = m.groupby(df.loc[m, 'C']).transform('sum').fillna('')
print (df)
A B C countifs
0 xx No 1
1 xx No 2
2 xx Yes 3 1
3 xx Yes 4 2
4 xx No 1
5 xx No 2
6 xx No 3
7 xx Yes 4 2
8 xx No 1
9 xx No 2
Here's one approach:这是一种方法:
y = df.B.eq('Yes')
df.loc[y, 'countifs'] = df[y].groupby('C').B.transform('count').values
print(df.fillna(''))
A B C countifs
0 xx No 1
1 xx No 2
2 xx Yes 3 1
3 xx Yes 4 2
4 xx No 1
5 xx No 2
6 xx No 3
7 xx Yes 4 2
8 xx No 1
9 xx No 2
Here is another approach, using numpy.where
, .value_counts()
, and .map()
:这是另一种方法,使用
numpy.where
、 .value_counts()
和.map()
:
>>> col_C_cnts = df[df.B == 'Yes']['C'].value_counts()
>>> df['countifs'] = np.where(df.B == 'Yes', df['C'].map(col_C_cnts), pd.np.nan)
>>> print(df.fillna(''))
A B C countifs
0 xx No 1
1 xx No 2
2 xx Yes 3 1
3 xx Yes 4 2
4 xx No 1
5 xx No 2
6 xx No 3
7 xx Yes 4 2
8 xx No 1
9 xx No 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.