Skip to content
Advertisement

Extract Value From Pandas Dataframe Based On Condition in Another Column

I am trying to develop some code that extracts the power price when a power plant starts up. To give an example refer to the following data frame.

JavaScript

Based on this I aiming to develop some code that would store in a dataframe the power price when the plant ops columns transitions from 0 to a number greater than 0 (i.e. when the power plant starts). In the case of the data above the output would look something along the lines of:

JavaScript

Hopefully this makes sense. Certainly welcome any advice or guidance you are able to provide.

Advertisement

Answer

Use DataFrame.melt with filter rows with shifted per groups equal 0 and also greater like 0 in boolean indexing:

JavaScript

Last if necessary change order of columns:

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