[英]Python Pandas: Counting the frequency of a specific value in each row of dataframe?
[英]Pandas - counting frequency of value for each value type in other column
所以数据看起来像这样:
type value
A 0
A 6
B 5
C 0
A 3
C 0
我想获取类型列中每种类型的值列中零的数量。 最好在新的 dataframe 中。 所以它看起来像这样:
type zero_count
A 1
B 0
C 2
最有效的方法是什么?
按Series.eq
比较==
列,通过Series.view
或Series.astype
转换为整数0/1
,然后按列df['type']
与sum
聚合:
df1 = df['value'].eq(0).view('i1').groupby(df['type']).sum().reset_index(name='zero_count')
df1 = df['value'].eq(0).astype(int).groupby(df['type']).sum().reset_index(name='zero_count')
print (df1)
type zero_count
0 A 1
1 B 0
2 C 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.