Issue
I have a data frame:
Dept_Name | Placed |
---|---|
A | 1 |
B | 0 |
C | 1 |
Where 'Placed' column has a boolean value I want to print the count of rows that have the value '1' in placed grouped by the Dept_Name
Dept_Name | Count(Placed == 1) |
---|---|
A | 3 |
B | 4 |
C | 0 |
Solution
If values are 0,1
or True/False
you can aggregate sum
, last for column Count
use Series.reset_index
:
df1 = df.groupby('Dept_Name')['Placed'].sum().reset_index(name='Count')
If test some non boolean values - e.g. count values 100
:
df2 = df['Placed'].eq(100).groupby(df['Dept_Name']).sum().reset_index(name='Count')
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.