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
x
8
1
data = {
2
'Power_Price': [10, 11,15, 33, 50, 10, 12, 20, 17],
3
'Plant_Ops_1': [0, 0, 10, 10, 10, 0, 0, 10, 10],
4
'Plant_Ops_2': [0, 0, 0, 50, 50, 0, 0, 0, 0]
5
}
6
7
df = pd.DataFrame (data, columns = ['Power_Price','Plant_Ops_1','Plant_Ops_2'])
8
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
1
7
1
data_out = {
2
'Plant': ['Plant_Ops_1', 'Plant_Ops_1', 'Plant_Ops_2'],
3
'Power_price': [15, 20, 33]
4
}
5
6
df_out = pd.DataFrame (data_out, columns = ['Plant','Power_price'])
7
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
1
9
1
df = df.melt('Power_Price', var_name='Plant')
2
3
df = df[df.groupby('Plant')['value'].shift().eq(0) & df['value'].gt(0)].drop('value',axis=1)
4
print (df)
5
Power_Price Plant
6
2 15 Plant_Ops_1
7
7 20 Plant_Ops_1
8
12 33 Plant_Ops_2
9
Last if necessary change order of columns:
JavaScript
1
2
1
df = df[["Plant", "Power_Price"]]
2