Count where Column is True, Grouped By Another in Pandas

Hanshan

Given a DataFrame like this:

dft = pd.DataFrame([[12, 'Bob', True], 
                   [123, 'Henry', False], 
                   [768, 'Bob', False]], 
                   columns=['TID', 'UID', 'TRUTH'])

Which looks like this:

   TID    UID  TRUTH
0   12    Bob   True
1  123  Henry  False
3  768    Bob  False

I want to aggregate over UID and count where TRUTH is True. So the output should look like:

     UID  TRUTH
0    Bob      1
1  Henry      0

I have already tried:

 dft.groupby('UID').agg({'TRUTH': pd.Series.nunique})  # counts all values T and F

I am conceptually struggling to see how to put the condition together with the aggregation.

Thanks for your help!

Joe T. Boka

Is this what you're looking for?

print(dft.groupby('UID')['TRUTH'].sum().astype(int))

Output:

UID
Bob      1
Henry    0
Name: TRUTH, dtype: int32

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Making a Count of Days Column Grouped by Another Column (pandas)

From Dev

Grouped count of combinations in Pandas column

From Dev

Pandas count values in one column where another column stays the same

From Dev

Count frequency of value in pandas column where values in another column are similar

From Dev

SQL: count rows where column = a value AND another column is the same as values in the group where the first condition is true?

From Dev

Sum a column in a pandas dataframe where a condition is met in one column, but grouped by another

From Dev

Managing count grouped by another column in python

From Dev

Pandas: compute the mean of a column grouped by another column

From Dev

Make a grouped column by sum of another column with pandas

From Dev

Pandas summing rows grouped by another column

From Dev

Count occurrences of list item in dataframe column, grouped by another column

From Dev

SQL add count column based on same table and grouped by another column

From Dev

Count all defined values in a DataFrame column where the corresponding values in another column are undefined in pandas

From Python

Find the frequent elements from a list in pandas column grouped by another column

From Dev

Normalize column in pandas dataframe by sum of grouped values of another column

From Dev

How to add a column with given values to a Pandas dataframe, grouped by another column?

From Dev

Add column that keeps count of distinct values grouped by a variable in pandas

From Dev

Count distinct rows where another column not the same

From Python

Is there a way in pandas to groupby and then count unique where another column has a specified value?

From Dev

Cumulative count where key equals value from another column in Python Pandas

From Dev

Count the total of a grouped by with pandas

From Java

How to calculate mean values grouped on another column in Pandas

From Dev

Complete date series grouped by another column in pandas and fill missing rows

From Dev

Pandas Grouping - Values as Percent of Grouped Totals Based on Another Column

From Dev

How to return value_counts() when grouped by another column in pandas

From Dev

How to count where column value is falsy in Pandas?

From Dev

Pandas Dataframe: Count unique words in a column and return count in another column

From Dev

pandas - create true/false column if column header is substring of another column

From Dev

Pandas: Increment or reset count based on another column

Related Related

  1. 1

    Making a Count of Days Column Grouped by Another Column (pandas)

  2. 2

    Grouped count of combinations in Pandas column

  3. 3

    Pandas count values in one column where another column stays the same

  4. 4

    Count frequency of value in pandas column where values in another column are similar

  5. 5

    SQL: count rows where column = a value AND another column is the same as values in the group where the first condition is true?

  6. 6

    Sum a column in a pandas dataframe where a condition is met in one column, but grouped by another

  7. 7

    Managing count grouped by another column in python

  8. 8

    Pandas: compute the mean of a column grouped by another column

  9. 9

    Make a grouped column by sum of another column with pandas

  10. 10

    Pandas summing rows grouped by another column

  11. 11

    Count occurrences of list item in dataframe column, grouped by another column

  12. 12

    SQL add count column based on same table and grouped by another column

  13. 13

    Count all defined values in a DataFrame column where the corresponding values in another column are undefined in pandas

  14. 14

    Find the frequent elements from a list in pandas column grouped by another column

  15. 15

    Normalize column in pandas dataframe by sum of grouped values of another column

  16. 16

    How to add a column with given values to a Pandas dataframe, grouped by another column?

  17. 17

    Add column that keeps count of distinct values grouped by a variable in pandas

  18. 18

    Count distinct rows where another column not the same

  19. 19

    Is there a way in pandas to groupby and then count unique where another column has a specified value?

  20. 20

    Cumulative count where key equals value from another column in Python Pandas

  21. 21

    Count the total of a grouped by with pandas

  22. 22

    How to calculate mean values grouped on another column in Pandas

  23. 23

    Complete date series grouped by another column in pandas and fill missing rows

  24. 24

    Pandas Grouping - Values as Percent of Grouped Totals Based on Another Column

  25. 25

    How to return value_counts() when grouped by another column in pandas

  26. 26

    How to count where column value is falsy in Pandas?

  27. 27

    Pandas Dataframe: Count unique words in a column and return count in another column

  28. 28

    pandas - create true/false column if column header is substring of another column

  29. 29

    Pandas: Increment or reset count based on another column

HotTag

Archive