Calculate difference and mean over groups in DataFrame

machinery

I have a Pandas DataFrame which looks as follows:

ID1    ID2     timestamp   x    y
0      0       43          1    40
0      0       53          20   41
0      0       63          21   41
0      1       73          5    100
0      1       75          6    99
0      1       83          7    87
1      0       100         34   23
1      0       200         0    0
1      0       210         0    22
1      0       222         22   15
2      0       300         22   15
2      1       450         22   15
2      1       451         22   15

Now, for each group of ID1 and ID2 I would like to calculate the time difference (max - min timestamp) and the mean of x and y. The resulting dataframe should look as follows:

ID1    ID2     timestamp   x    y
0      0       20          14   40.6
0      1       10          6    95.3
1      0       110         14   15
2      0       300         22   15
2      1       1           22   15

How can this be done? I could use df.groupby(["ID1", "ID2"]) to group but I don't know how to calculate the difference and mean in situ.

JD D

Should be something like:

df.groupby(["ID1", "ID2"]).agg({
   'timestamp': lambda x: x.max() - x.min(),
   'x': 'mean',
   'y': 'mean'
}).reset_index()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculate date difference of dataframe groups

From Dev

Calculate the mean difference within and between groups

From Dev

Calculate Mean on Multiple Groups

From Dev

Calculate difference between groups

From Python

Calculate mean of specific groups in pandas DataFrame (using axis=1) - how to get the specific groups?

From Dev

Calculate mean difference values

From Dev

Iterating over groups into a dataframe

From Dev

How to calculate mean in a dataframe?

From Dev

Mean difference between groups in R

From Dev

Pandas Dataframe calculate Time difference for each group and Time difference between two different groups

From Dev

Calculate difference sequentially by groups in pandas

From Dev

Calculate row difference within groups

From Dev

calculate the difference between groups as a percentage

From Dev

Looping over groups in a grouped dataframe

From Dev

Iterating over rows and groups in dataframe

From Dev

How to iterate over dataframe using character vector and calculate the mean for matching items in R

From Dev

Pandas: calculate ratio between groups over time

From Dev

How to calculate mean of grouped dataframe?

From Dev

Calculating a difference for groups within dataframe

From Dev

Conditional mean over a Pandas DataFrame

From Dev

Rolling mean returns over DataFrame

From Dev

Calculate the difference between groups in Power BI

From Dev

Calculate Average Time Difference in Groups Pandas Python

From Python

Is there a way to calculate std and mean over two parameters?

From Dev

Calculate mean cell values over different files

From Dev

Pyspark calculate mean over whole column with list

From Dev

Running a for loop in r over columns to calculate mean

From Dev

How to calculate difference between two groups considering other columns groups

From Dev

Replace a column values with its mean of groups in dataframe

Related Related

  1. 1

    Calculate date difference of dataframe groups

  2. 2

    Calculate the mean difference within and between groups

  3. 3

    Calculate Mean on Multiple Groups

  4. 4

    Calculate difference between groups

  5. 5

    Calculate mean of specific groups in pandas DataFrame (using axis=1) - how to get the specific groups?

  6. 6

    Calculate mean difference values

  7. 7

    Iterating over groups into a dataframe

  8. 8

    How to calculate mean in a dataframe?

  9. 9

    Mean difference between groups in R

  10. 10

    Pandas Dataframe calculate Time difference for each group and Time difference between two different groups

  11. 11

    Calculate difference sequentially by groups in pandas

  12. 12

    Calculate row difference within groups

  13. 13

    calculate the difference between groups as a percentage

  14. 14

    Looping over groups in a grouped dataframe

  15. 15

    Iterating over rows and groups in dataframe

  16. 16

    How to iterate over dataframe using character vector and calculate the mean for matching items in R

  17. 17

    Pandas: calculate ratio between groups over time

  18. 18

    How to calculate mean of grouped dataframe?

  19. 19

    Calculating a difference for groups within dataframe

  20. 20

    Conditional mean over a Pandas DataFrame

  21. 21

    Rolling mean returns over DataFrame

  22. 22

    Calculate the difference between groups in Power BI

  23. 23

    Calculate Average Time Difference in Groups Pandas Python

  24. 24

    Is there a way to calculate std and mean over two parameters?

  25. 25

    Calculate mean cell values over different files

  26. 26

    Pyspark calculate mean over whole column with list

  27. 27

    Running a for loop in r over columns to calculate mean

  28. 28

    How to calculate difference between two groups considering other columns groups

  29. 29

    Replace a column values with its mean of groups in dataframe

HotTag

Archive