Using first row in Pandas groupby dataframe to calculate cumulative difference

D hagen

I have the following grouped dataframe based on daily data

Studentid  Year Month BookLevel

 JSmith    2015  12    1.4
           2016   1    1.6
                  2    1.8
                  3    1.2
                  4    2.0

 MBrown    2016   1    3.0        
                  2    3.2
                  3    3.6 

I want to calculate the difference from the starting point in BookLevel for each Studentid. The current BookLevel is a .max calculation from the GroupBy to get the highest bookLevel for each month for each student

What I am looking for is something like this:

 Studentid    Year   Month   BookLevel    Progress Since Start

  JSmith      2015     12       1.4         0 (or NAN)
              2016      1       1.6        .2
                        2       1.8        .4
                        3       1.2       -.2
                        4       2.0        .6

              2016      1       3.0         0 (or NAN)   
  MBrown                2       3.2        .2
                        3       3.6        .6

I'm new to Python/Pandas and have tried a number of things and nothing comes close.

EdChum

OK, this should work, if we groupby on the first level and subtract BookLevel from the series returned by calling transform with first then we can add this as the new desired column:

In [47]:
df['ProgressSinceStart'] = df['BookLevel'] - df.groupby(level='Studentid')['BookLevel'].transform('first')
df

Out[47]:
                      BookLevel  ProgressSinceStart
Studentid Year Month                               
JSmith    2015 12           1.4                 0.0
          2016 1            1.6                 0.2
               2            1.8                 0.4
               3            1.2                -0.2
               4            2.0                 0.6
MBrown    2016 1            3.0                 0.0
               2            3.2                 0.2
               3            3.6                 0.6

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using first row in Pandas groupby dataframe to calculate cumulative difference

From Dev

Calculate STD manually using Groupby Pandas DataFrame

From Dev

Calculate STD manually using Groupby Pandas DataFrame

From Dev

Pandas dataframe apply refer to previous row to calculate difference

From Dev

Using pandas groupby and apply for cumulative integration

From Dev

calculate row difference groupwise in pandas

From Dev

pandas DataFrame and pandas.groupby to calculate Salaries

From Dev

Pandas Cumulative Sum using Current Row as Condition

From Dev

Pandas dataframe groupby to calculate population standard deviation

From Dev

Calculate difference from a reference row in pandas (python)

From Dev

Pandas Cumulative Sum of Difference Between Value Counts in Two Dataframe Columns

From Dev

pandas: Divide DataFrame last row by first row

From Dev

Converting Pandas groupby.groups result into dataframe, using index tuple value as row and columns name of dataframe

From Dev

pandas dataframe groupby and get nth row

From Dev

Pandas dataframe groupby and combine multiple row values

From Dev

pandas dataframe groupby and get nth row

From Dev

pandas dataframe filtering row like groupby

From Dev

Pandas DataFrame: Calculate percentage difference between rows?

From Dev

Using Pandas groupby to calculate many slopes

From Dev

pandas DataFrame cumulative value

From Java

Pandas groupby transform cumulative with conditions

From Dev

Calculate cumulative sum forward pandas

From Dev

How to calculate total time difference for rows with same name using a pandas dataframe?

From Dev

Row wise mean difference in a Pandas DataFrame

From Java

Pandas dataframe get first row of each group

From Dev

Extract first and last row of a dataframe in pandas

From Dev

Selecting the values of the first row in pandas' DataFrame

From Dev

Selecting the values of the first row in pandas' DataFrame

From Dev

Find first zero in row of pandas DataFrame

Related Related

  1. 1

    Using first row in Pandas groupby dataframe to calculate cumulative difference

  2. 2

    Calculate STD manually using Groupby Pandas DataFrame

  3. 3

    Calculate STD manually using Groupby Pandas DataFrame

  4. 4

    Pandas dataframe apply refer to previous row to calculate difference

  5. 5

    Using pandas groupby and apply for cumulative integration

  6. 6

    calculate row difference groupwise in pandas

  7. 7

    pandas DataFrame and pandas.groupby to calculate Salaries

  8. 8

    Pandas Cumulative Sum using Current Row as Condition

  9. 9

    Pandas dataframe groupby to calculate population standard deviation

  10. 10

    Calculate difference from a reference row in pandas (python)

  11. 11

    Pandas Cumulative Sum of Difference Between Value Counts in Two Dataframe Columns

  12. 12

    pandas: Divide DataFrame last row by first row

  13. 13

    Converting Pandas groupby.groups result into dataframe, using index tuple value as row and columns name of dataframe

  14. 14

    pandas dataframe groupby and get nth row

  15. 15

    Pandas dataframe groupby and combine multiple row values

  16. 16

    pandas dataframe groupby and get nth row

  17. 17

    pandas dataframe filtering row like groupby

  18. 18

    Pandas DataFrame: Calculate percentage difference between rows?

  19. 19

    Using Pandas groupby to calculate many slopes

  20. 20

    pandas DataFrame cumulative value

  21. 21

    Pandas groupby transform cumulative with conditions

  22. 22

    Calculate cumulative sum forward pandas

  23. 23

    How to calculate total time difference for rows with same name using a pandas dataframe?

  24. 24

    Row wise mean difference in a Pandas DataFrame

  25. 25

    Pandas dataframe get first row of each group

  26. 26

    Extract first and last row of a dataframe in pandas

  27. 27

    Selecting the values of the first row in pandas' DataFrame

  28. 28

    Selecting the values of the first row in pandas' DataFrame

  29. 29

    Find first zero in row of pandas DataFrame

HotTag

Archive