I have the following pandas df:
df
price max maxperhour
Site Commodity Type
Mid Biomass Stock 6.0 1.500000e+15 1.500000e+15
CO2 Env 0.0 1.500000e+15 1.500000e+15
Coal Stock 7.0 1.500000e+15 1.500000e+15
Elec Demand NaN NaN NaN
Gas Stock 27.0 1.500000e+15 1.500000e+15
Hydro SupIm NaN NaN NaN
Lignite Stock 4.0 1.500000e+15 1.500000e+15
Solar SupIm NaN NaN NaN
Wind SupIm NaN NaN NaN
I would like to filter above mentioned df and create a list of Commodity
items as a list, when Site == 'Mid'
and Type == ('Stock' or 'Demand')
.
so following list should be created with some pandas filtering function:
df.somefunction()
['Biomass', 'Coal', 'Gas', 'Lignite', 'Elec']
How would I achieve this?
Lastly if it is possible I would like to have 'Elec'
as the last element, what I mean is; when the list is created, 'Elec'
would be probably the third element of the list like:
['Biomass', 'Coal', 'Elec', 'Gas', 'Lignite']
However, it would be best if I can get 'Elec'
as the last element like:
['Biomass', 'Coal', 'Gas', 'Lignite', 'Elec']
since it is the only element with Type == 'Demand'
From @jezrael
df[(df.index.get_level_values('Site') == 'Mid') & (df.index.get_level_values('Type') == 'Stock')].index.remove_unused_levels().get_level_values('Commodity').tolist()