groupby, sum and count to one table

Heisenberg

I have a dataframe below

df=pd.DataFrame({"A":np.random.randint(1,10,9),"B":np.random.randint(1,10,9),"C":list('abbcacded')})

   A  B  C
0  9  6  a
1  2  2  b
2  1  9  b
3  8  2  c
4  7  6  a
5  3  5  c
6  1  3  d
7  9  9  e
8  3  4  d

I would like to get grouping result (with key="C" column) below,and the row c d and e is dropped intentionally.

   number   A_sum   B_sum
a   2        16       15
b   2        3        11

this is 2row*3column dataframe. the grouping key is column C. And The column "number"represents the count of each letter(a and b). A_sum and B_sum represents grouping sum of letters in column C.

I guess we should use method groupby but how can I get this data summary table ?

Psidom

One option is to count the size and sum the columns for each group separately and then join them by index:

df.groupby("C")['A'].agg({"number": 'size'}).join(df.groupby('C').sum())

    number  A   B
# C         
# a     2   11  8
# b     2   14  12
# c     2   8   5
# d     2   11  12
# e     1   7   2

You can also do df.groupby('C').agg(["sum", "size"]) which gives an extra duplicated size column, but if you are fine with that, it should also work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

groupby, sum and count to one table

From Dev

SQL: Sum and groupby, criteria for groupby in another table

From Dev

Selecting SUM+COUNT from one table and COUNT from another in Single query

From Dev

SQL : Count(*) and groupby with different table

From Dev

PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

From Dev

PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

From Dev

Sum of marks in one table

From Dev

Converting SQL to Linq with groupby, sum and count

From Dev

Julia groupBy name and sum up count

From Dev

laravel use count/sum method after groupBy

From Dev

Converting SQL to Linq with groupby, sum and count

From Dev

laravel use count/sum method after groupBy

From Dev

Pandas DataFrame GroupBy sum/count to new DataFrame

From Dev

MySQL: Sum () and Count in one query?

From Dev

MySQL: Sum () and Count in one query?

From Dev

Count one table and ORDER BY it

From Dev

MySql Sum and Count for simple table

From Dev

MySQL join / sum value from one table colum with a count values from another

From Dev

Pandas Groupby and Sum Only One Column

From Dev

Pandas Groupby and Sum Only One Column

From Dev

one query with groupby / count / select new

From Dev

one query with groupby / count / select new

From Dev

Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

From Dev

Query count, sum, with condition in one query with mysql

From Dev

SQL unify COUNT and SUM in one query

From Dev

Join two table and sum count issue

From Dev

Sum and count aggregate functions in dynamic pivot table

From Dev

Select * as well as count/sum from another table

From Dev

Join two table and sum count issue

Related Related

  1. 1

    groupby, sum and count to one table

  2. 2

    SQL: Sum and groupby, criteria for groupby in another table

  3. 3

    Selecting SUM+COUNT from one table and COUNT from another in Single query

  4. 4

    SQL : Count(*) and groupby with different table

  5. 5

    PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

  6. 6

    PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

  7. 7

    Sum of marks in one table

  8. 8

    Converting SQL to Linq with groupby, sum and count

  9. 9

    Julia groupBy name and sum up count

  10. 10

    laravel use count/sum method after groupBy

  11. 11

    Converting SQL to Linq with groupby, sum and count

  12. 12

    laravel use count/sum method after groupBy

  13. 13

    Pandas DataFrame GroupBy sum/count to new DataFrame

  14. 14

    MySQL: Sum () and Count in one query?

  15. 15

    MySQL: Sum () and Count in one query?

  16. 16

    Count one table and ORDER BY it

  17. 17

    MySql Sum and Count for simple table

  18. 18

    MySQL join / sum value from one table colum with a count values from another

  19. 19

    Pandas Groupby and Sum Only One Column

  20. 20

    Pandas Groupby and Sum Only One Column

  21. 21

    one query with groupby / count / select new

  22. 22

    one query with groupby / count / select new

  23. 23

    Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

  24. 24

    Query count, sum, with condition in one query with mysql

  25. 25

    SQL unify COUNT and SUM in one query

  26. 26

    Join two table and sum count issue

  27. 27

    Sum and count aggregate functions in dynamic pivot table

  28. 28

    Select * as well as count/sum from another table

  29. 29

    Join two table and sum count issue

HotTag

Archive