Make a grouped column by sum of another column with pandas

Mexyn

I have this data set:

+===+=======+======+=======+=======+
|   | Group | Cost | Name1 | Name2 |
+===+=======+======+=======+=======+
| 0 | G1    | 1574 | N1A   | N2A   |
+---+-------+------+-------+-------+
| 1 | G2    | 1322 | N1B   | N2B   |
+---+-------+------+-------+-------+
| 2 | G3    | 1188 | N1C   | N2C   |
+---+-------+------+-------+-------+
| 3 | G3    |  942 | N1D   | N2D   |
+---+-------+------+-------+-------+
| 4 | G4    |  838 | N1E   | N2E   |
+---+-------+------+-------+-------+
| 5 | G5    |    5 | N1F   | N2F   |
+---+-------+------+-------+-------+
| 6 | G5    |    4 | N1F   | N2G   |
+---+-------+------+-------+-------+
| 7 | G5    |    3 | N1G   | N2H   |
+---+-------+------+-------+-------+

Now i want to group by "Group" and add a grouped column with the sum of column "Cost" for each group. Dont know how to explain, so here is the expected result:

+===+=======+======+======+=======+=======+
|   | Group | Sum  | Cost | Name1 | Name2 |
+===+=======+======+======+=======+=======+
| 0 | G1    | 1574 | 1574 | N1A   | N2A   |
+---+-------+------+------+-------+-------+
| 1 | G2    | 1322 | 1322 | N1B   | N2B   |
+---+-------+------+------+-------+-------+
| 2 | G3    | 2130 | 1188 | N1C   | N2C   |
|   |       |      +------+-------+-------+
|   |       |      |  942 | N1D   | N2D   |
+---+-------+------+------+-------+-------+
| 3 | G4    |  838 |  838 | N1E   | N2E   |
+---+-------+------+------+-------+-------+
| 4 | G5    |   12 |    5 | N1F   | N2F   |
|   |       |      +------+-------+-------+
|   |       |      |    4 | N1F   | N2G   |
|   |       |      +------+-------+-------+
|   |       |      |    3 | N1G   | N2H   |
+---+-------+------+------+-------+-------+

How can i achieve this with pandas? Is that even possible? Sorry i am new to this stuff

jezrael

Use GroupBy.transform with sum and then for display your way create MultiIndex by DataFrame.set_index, but 'missing' values in MulitIndex are only not displaing:

df['Sum'] = df.groupby('Group')['Cost'].transform('sum')
df = df.set_index(['Group','Sum','Cost'])

Or:

df1 = (df.assign(Sum = df.groupby('Group')['Cost'].transform('sum'))
         .set_index(['Group','Sum','Cost']))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Divide the column by the sum grouped by another column

From Dev

Sum pandas dataframe column values grouped by another column then update row with sum and remove duplicates

From Dev

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

From Dev

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

From Dev

Pandas - How to make a groupment in which a new column is the result of (sum of a column)/(number of itens grouped)?

From Dev

How to sum values grouped by a categorical column in pandas?

From Dev

How to divide elements in pandas column by grouped sum

From Dev

SQL - Sum of specific column grouped by a specific value of another column

From Dev

Sum of column when grouped by another column, but with different indexes

From Dev

Count where Column is True, Grouped By Another in Pandas

From Dev

Pandas summing rows grouped by another column

From Dev

Sum column based on another column in Pandas DataFrame

From Dev

Sum Values by Grouped Column

From Dev

SUM a grouped column

From Python

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

From Dev

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

From Dev

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

From Dev

Average on column grouped by another column

From Dev

DAX - Sum column values from another table grouped by

From Dev

make sum of one column and join it with another table

From Dev

Divide elements of column by a sum of elements (of same column) grouped by elements of another column

From Dev

Pandas Grouped and sum data column that separated by special character

From Dev

Pandas new column based in sum a column from another pandas

From Dev

Pandas groupby() on one column and then sum on another

From Dev

Getting the sum of column grouped by date

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

Related Related

  1. 1

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

  2. 2

    Divide the column by the sum grouped by another column

  3. 3

    Sum pandas dataframe column values grouped by another column then update row with sum and remove duplicates

  4. 4

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

  5. 5

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

  6. 6

    Pandas - How to make a groupment in which a new column is the result of (sum of a column)/(number of itens grouped)?

  7. 7

    How to sum values grouped by a categorical column in pandas?

  8. 8

    How to divide elements in pandas column by grouped sum

  9. 9

    SQL - Sum of specific column grouped by a specific value of another column

  10. 10

    Sum of column when grouped by another column, but with different indexes

  11. 11

    Count where Column is True, Grouped By Another in Pandas

  12. 12

    Pandas summing rows grouped by another column

  13. 13

    Sum column based on another column in Pandas DataFrame

  14. 14

    Sum Values by Grouped Column

  15. 15

    SUM a grouped column

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

    Average on column grouped by another column

  20. 20

    DAX - Sum column values from another table grouped by

  21. 21

    make sum of one column and join it with another table

  22. 22

    Divide elements of column by a sum of elements (of same column) grouped by elements of another column

  23. 23

    Pandas Grouped and sum data column that separated by special character

  24. 24

    Pandas new column based in sum a column from another pandas

  25. 25

    Pandas groupby() on one column and then sum on another

  26. 26

    Getting the sum of column grouped by date

  27. 27

    How to calculate mean values grouped on another column in Pandas

  28. 28

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

  29. 29

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

HotTag

Archive