[英]python pandas: how to group by and count with a condition for every value in a column?
I have table like this: 我有这样的表:
d group
1 a
2 b
3 a
4 c
5 f
and I like to iterate over values of d and count number of rows that have group=a . 我想遍历d的值并计算具有group = a的行数。
Here is what I am doing now, but It does not work: 这是我现在正在做的事情,但是它不起作用:
for index,row in df.iterrows():
for x in (1,5):
if row['d'] > x:
row['tp'] = df.groupby('group').agg(lambda x:x.manual_type=='a')
Can anybody help? 有人可以帮忙吗?
try: 尝试:
df['group'].value_counts()['a']
in general, you should NEVER use for loops in pandas. 通常,您不应该在熊猫中使用循环。 it's inefficient and usually recreating some existing functionality in the package. 它效率低下,通常会在软件包中重新创建一些现有功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.