Skip to content
Advertisement

pandas groupby dataframes, calculate diffs between consecutive rows

Using pandas, I open some csv files in a loop and set the index to the cycleID column, except the cycleID column is not unique. See below:

JavaScript

This prints the 2 columns (cycleID and mean) of the dataframe I am interested in for further computations:

JavaScript

The objective is to use the rows corresponding to the same cycleID and calculate the difference between the mean column values. So, if there are 8 rows in the table, the final array or list would store 4 values.

I want to make it scalable as well where there can be 3 or more rows with the same cycleIDs. In that case, each cycleID could have 2 or more mean differences.


Update: Instead of creating a new ques about it, I thought I’d add here. I used the diff and groupby approach as mentioned in the solution. It works great but I have this extra need to save one of the mean values (odd row or even row doesn’t matter) in a new column and make that part of the new data frame as well. How do I do that?

Advertisement

Answer

You can use groupby

JavaScript

output

JavaScript

UPDATE

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement