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 .
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.