update pandas.DataFrame within a group after .groupby()

Sergiy Matusevych

I have the following pandas.DataFrame:

                                                          time
offset   ts                      op                           
0.000000 2015-10-27 18:31:40.318 BuildIndex            282.604
                                 Compress              253.649
                                 Decompress              2.953
                                 Deserialize             0.063
                                 InsertIndex             1.343
4.960683 2015-10-27 18:36:37.959 BuildIndex            312.249
                                 Compress              280.747
                                 Decompress              2.844
                                 Deserialize             0.110
                                 InsertIndex             0.907

Now I need to update the dataframe (in-place is OK): for each group, subtract the time for op == 'Compress' from the one for op == 'BuildIndex' - within the same group.

What is the most elegant way to do it in pandas?

Andy Hayden

I'd use xs (cross-section) to do this:

In [11]: df1.xs("Compress", level="op")
Out[11]:
                                     time
offset   ts
0.000000 2015-10-27 18:31:40.318  253.649
4.960683 2015-10-27 18:36:37.959  280.747

In [12]: df1.xs("BuildIndex", level="op")
Out[12]:
                                     time
offset   ts
0.000000 2015-10-27 18:31:40.318  282.604
4.960683 2015-10-27 18:36:37.959  312.249

In [13]: df1.xs("BuildIndex", level="op") - df1.xs("Compress", level="op")
Out[13]:
                                    time
offset   ts
0.000000 2015-10-27 18:31:40.318  28.955
4.960683 2015-10-27 18:36:37.959  31.502

The subtraction works on the index labels (in this case offset and ts), so no need to group.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculation within Pandas dataframe group

From Dev

Pandas - return a dataframe after groupby

From Dev

Sorting Pandas dataframe data within Groupby groups

From Dev

Groupby items within a list in a Pandas DataFrame

From Dev

using groupby on pandas dataframe to group by financial year

From Java

How to group dataframe rows into list in pandas groupby

From Dev

Pandas Dataframe GroupBy - Displaying Group Statistics

From Dev

using groupby on pandas dataframe to group by financial year

From Dev

Sample each group after pandas groupby

From Dev

Pandas: how to get a particular group after groupby?

From Dev

add timedelta data within a group in pandas dataframe

From Dev

Row operations within a group of a pandas dataframe

From Dev

Convert from irregular frequency to monthly within groupby objects in pandas dataframe

From Java

if condition within groupby pandas

From Dev

ranks within groupby in pandas

From Dev

Pandas interpolate within a groupby

From Dev

Python Pandas, setting groupby() group labels as index in a new dataframe

From Dev

Pandas Dataframe: get average of first rows of each subgroup within a group

From Dev

How to filter a pandas dataframe to just the rows that show change within a group?

From Dev

pandas - keep only True values after groupby a DataFrame

From Dev

How to split a pandas dataframe into many columns after groupby

From Dev

Using print to access individual cells of pandas dataframe after groupby

From Dev

Rename columns after pandas.DataFrame.groupby.apply()

From Dev

How to do sorting after groupby and aggregation on a Pandas Dataframe

From Dev

Python 2.7: DataFrame groupby and find find the percentage distribution of values within group

From Dev

pandas dataframe groupby summation

From Dev

Reorder pandas groupby dataframe

From Dev

pandas, dataframe, groupby, std

From Dev

groupby - python pandas dataframe

Related Related

  1. 1

    Calculation within Pandas dataframe group

  2. 2

    Pandas - return a dataframe after groupby

  3. 3

    Sorting Pandas dataframe data within Groupby groups

  4. 4

    Groupby items within a list in a Pandas DataFrame

  5. 5

    using groupby on pandas dataframe to group by financial year

  6. 6

    How to group dataframe rows into list in pandas groupby

  7. 7

    Pandas Dataframe GroupBy - Displaying Group Statistics

  8. 8

    using groupby on pandas dataframe to group by financial year

  9. 9

    Sample each group after pandas groupby

  10. 10

    Pandas: how to get a particular group after groupby?

  11. 11

    add timedelta data within a group in pandas dataframe

  12. 12

    Row operations within a group of a pandas dataframe

  13. 13

    Convert from irregular frequency to monthly within groupby objects in pandas dataframe

  14. 14

    if condition within groupby pandas

  15. 15

    ranks within groupby in pandas

  16. 16

    Pandas interpolate within a groupby

  17. 17

    Python Pandas, setting groupby() group labels as index in a new dataframe

  18. 18

    Pandas Dataframe: get average of first rows of each subgroup within a group

  19. 19

    How to filter a pandas dataframe to just the rows that show change within a group?

  20. 20

    pandas - keep only True values after groupby a DataFrame

  21. 21

    How to split a pandas dataframe into many columns after groupby

  22. 22

    Using print to access individual cells of pandas dataframe after groupby

  23. 23

    Rename columns after pandas.DataFrame.groupby.apply()

  24. 24

    How to do sorting after groupby and aggregation on a Pandas Dataframe

  25. 25

    Python 2.7: DataFrame groupby and find find the percentage distribution of values within group

  26. 26

    pandas dataframe groupby summation

  27. 27

    Reorder pandas groupby dataframe

  28. 28

    pandas, dataframe, groupby, std

  29. 29

    groupby - python pandas dataframe

HotTag

Archive