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?
I need count 2 columns (lambda with two arguments) as the example:
Pandas dataframe groupby func
, in the columnkey2
like this:df.groupby('key1')['key2'].apply(lambda x: x[x == 'one'].count())
Option 1
Option 2
Option 3
Option 4
Option 5
I think you need add condition first:
Or use
categorical
withkey1
, then missing value is added bysize
:If need all combinations:
You can do this with applying groupby() on both keys and unstack().
Maybe not the fastest solution, but you can create new data frame with column of ones if key2 is equal to 'one'.
And then aggregate it.
You can count the occurence of 'one' for the groupby dataframe, in the column 'key2' like this:
df.groupby('key1')['key2'].apply(lambda x: x[x == 'one'].count())
yield