I have the following dataframe:
key1 key2
0 a one
1 a two
2 b one
3 b two
4 a one
5 c two
Now, I want to group the dataframe by the key1
and count the column key2
with the value "one"
to get this result:
key1
0 a 2
1 b 1
2 c 0
I just get the usual count with:
df.groupby(['key1']).size()
But I don't know how to insert the condition.
I tried things like this:
df.groupby(['key1']).apply(df[df['key2'] == 'one'])
But I can't get any further. How can I do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…