How to sum within a group of values and then take the difference from another group?

MEhsan

Let say I have this simplified dataframe with three variables:

ID    sample  test_result
P1    Normal           9
P1    Normal           18
P2    Normal           7
P2    Normal           16
P3    Normal           2
P3    Normal           11
P1     Tumor           6
P1     Tumor           15
P2     Tumor           5
P2     Tumor           15
P3     Tumor           3
P3     Tumor           12

I want to know how to sum the test_result values for each identical ID in each sample type (i.e. Normal, Tumor). Then I want to then take the difference between the summed normal and tumor test_result values.

I have tried using groupby on sample column and then use the diff() method on test_result column but that did not work. I guess I need to know how to do apply the .sum() first, but not sure how.

Here is what I have tried:

df.groupby('sample')['test_result'].diff()

The output I am expecting is like:

ID   test_result
P1             6 # (the sum of P1 Normal = 27) - (the sum of P1 Tumor = 21)  
P2             3
P3            -2 

Any idea how to tackle this?

jezrael

Use groupby with sum and reshape by unstack:

df = df.groupby(['ID','sample'])['test_result'].sum().unstack()

Or pivot_table:

df = df.pivot_table(index='ID',columns='sample', values='test_result', aggfunc='sum')

and then subtract columns:

df['new'] = df['Normal'] - df['Tumor']
print (df)
sample  Normal  Tumor  new
ID                        
P1          27     21    6
P2          23     20    3
P3          13     15   -2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to sum values from one index to another in pyspark with group by

From Dev

R - Sum within group and only if another variable has consecutive values

From Dev

How to group and sum values from array of objects?

From Dev

Sum the values of a column based on a group of values from another column

From Dev

Group by a column from csv and sum the values from another (PHP)

From Dev

How to spread the values to blank "" within a specific group from another column R

From Dev

Sum group values by lubridate %within% interval

From Dev

How to sum values of one column and group them by another column

From Dev

How to sum the values in a column based on another column or different group?

From Python

Pandas Get Max values within each group, by group sum

From Dev

How to group and sum integer values

From Dev

Display sum of values and group by another column within a column chart in Power Bi

From Dev

Group and sum items from array within foreach

From Dev

Take difference between observations within same group with a reference observation

From Dev

Error when trying to take the difference between observations within a group

From Dev

pandas - take N last values from a group

From Dev

MySQL query to group by and then take difference based on criteria instead of sum

From Dev

Pandas Group by sum of all the values of the group and another column as comma separated

From Dev

How to SUM the values from group in SSRS which are distinct

From Dev

How to select the sum() of a group of rows and the sum() of another group

From Dev

Calculate DateTime Difference Sum within Group Linq to Entity

From Dev

Group by with difference and sum

From Dev

How to pass a set of values from one Jmeter Thread group to another

From Dev

Sum values from two columns in the group if not duplicate

From Dev

Sum Values From Specific Group of Rows - SQL

From Dev

sum values from a previous row by group

From Dev

How do I take data from one observation and apply it to one other observation within a group?

From Dev

C# LINQ Group and sum List<T> then Group and sum another list within the first List

From Dev

Postgresql how can I sum(values) and group them by the month within jsonb?

Related Related

  1. 1

    How to sum values from one index to another in pyspark with group by

  2. 2

    R - Sum within group and only if another variable has consecutive values

  3. 3

    How to group and sum values from array of objects?

  4. 4

    Sum the values of a column based on a group of values from another column

  5. 5

    Group by a column from csv and sum the values from another (PHP)

  6. 6

    How to spread the values to blank "" within a specific group from another column R

  7. 7

    Sum group values by lubridate %within% interval

  8. 8

    How to sum values of one column and group them by another column

  9. 9

    How to sum the values in a column based on another column or different group?

  10. 10

    Pandas Get Max values within each group, by group sum

  11. 11

    How to group and sum integer values

  12. 12

    Display sum of values and group by another column within a column chart in Power Bi

  13. 13

    Group and sum items from array within foreach

  14. 14

    Take difference between observations within same group with a reference observation

  15. 15

    Error when trying to take the difference between observations within a group

  16. 16

    pandas - take N last values from a group

  17. 17

    MySQL query to group by and then take difference based on criteria instead of sum

  18. 18

    Pandas Group by sum of all the values of the group and another column as comma separated

  19. 19

    How to SUM the values from group in SSRS which are distinct

  20. 20

    How to select the sum() of a group of rows and the sum() of another group

  21. 21

    Calculate DateTime Difference Sum within Group Linq to Entity

  22. 22

    Group by with difference and sum

  23. 23

    How to pass a set of values from one Jmeter Thread group to another

  24. 24

    Sum values from two columns in the group if not duplicate

  25. 25

    Sum Values From Specific Group of Rows - SQL

  26. 26

    sum values from a previous row by group

  27. 27

    How do I take data from one observation and apply it to one other observation within a group?

  28. 28

    C# LINQ Group and sum List<T> then Group and sum another list within the first List

  29. 29

    Postgresql how can I sum(values) and group them by the month within jsonb?

HotTag

Archive