Selección de filas en un dataframe MultiIndex por índice sin perder ningún nivel

Me gustaría seleccionar una fila llamada ‘Media’, sin perder su índice ‘Sitio’

El siguiente código muestra el dataframe:

m.commodity 

  price max maxperstep Site Commodity Type Mid Biomass Stock 6.0 inf inf CO2 Env 0.0 inf inf Coal Stock 7.0 inf inf Elec Demand NaN NaN NaN Gas Stock 27.0 inf inf Hydro SupIm NaN NaN NaN Lignite Stock 4.0 inf inf Slack Stock 999.0 inf inf Solar SupIm NaN NaN NaN Wind SupIm NaN NaN NaN North Biomass Stock 6.0 inf inf CO2 Env 0.0 inf inf Coal Stock 7.0 inf inf Elec Demand NaN NaN NaN Gas Stock 27.0 inf inf Hydro SupIm NaN NaN NaN Lignite Stock 4.0 inf inf Slack Stock 999.0 inf inf Solar SupIm NaN NaN NaN Wind SupIm NaN NaN NaN South Biomass Stock 6.0 inf inf CO2 Env 0.0 inf inf Coal Stock 7.0 inf inf Elec Demand NaN NaN NaN Gas Stock 27.0 inf inf Hydro SupIm NaN NaN NaN Lignite Stock 4.0 inf inf Slack Stock 999.0 inf inf Solar SupIm NaN NaN NaN Wind SupIm NaN NaN NaN 

El resultado deseado sería el siguiente:

  price max maxperstep Site Commodity Type Mid Biomass Stock 6.0 inf inf CO2 Env 0.0 inf inf Coal Stock 7.0 inf inf Elec Demand NaN NaN NaN Gas Stock 27.0 inf inf Hydro SupIm NaN NaN NaN Lignite Stock 4.0 inf inf Slack Stock 999.0 inf inf Solar SupIm NaN NaN NaN Wind SupIm NaN NaN NaN 

Las siguientes respuestas dan el resultado deseado:

 m.commodity.xs('Mid', drop_level=False) m.commodity.loc[['Mid']] m.commodity.loc['Mid', :, :] 

ty MaxU, COLDSPEED y jezrael por las respuestas 🙂

También puedes usar loc con dobles tirantes.

 df.loc[['Mid']] price max maxperstep Site Commodity Type Mid Biomass Stock 6.0 inf inf CO2 Env 0.0 inf inf Coal Stock 7.0 inf inf Elec Demand NaN NaN NaN Gas Stock 27.0 inf inf Hydro SupIm NaN NaN NaN Lignite Stock 4.0 inf inf Slack Stock 999.0 inf inf Solar SupIm NaN NaN NaN Wind SupIm NaN NaN NaN 

En su caso, supongo que sería m.commodity.loc[['Mid']] .

Cuando se habla de loc versus ix es que este último está en desuso, use loc / iloc / iat / xs para la indexación.

ix hace suposiciones sobre lo que se pasa y acepta tags o posiciones. loc es puramente basado en tags, mientras que iloc es puramente indexado (basado en posicionales)

 In [59]: df.loc['Mid', :, :] Out[59]: price max maxperstep Site Commodity Type Mid Biomass Stock 6.0 inf inf CO2 Env 0.0 inf inf Coal Stock 7.0 inf inf Elec Demand NaN NaN NaN Gas Stock 27.0 inf inf Hydro SupIm NaN NaN NaN Lignite Stock 4.0 inf inf Slack Stock 999.0 inf inf Solar SupIm NaN NaN NaN Wind SupIm NaN NaN NaN 

Creo que necesitas xs :

 df = m.commodity.xs('Mid', drop_level=False) print (df) b price max maxperstep Site Commodity Type Mid Biomass Stock 6.0 inf inf CO2 Env 0.0 inf inf Coal Stock 7.0 inf inf Elec Demand NaN NaN NaN Gas Stock 27.0 inf inf Hydro SupIm NaN NaN NaN Lignite Stock 4.0 inf inf Slack Stock 999.0 inf inf Solar SupIm NaN NaN NaN Wind SupIm NaN NaN NaN 

Para usted, otra pregunta es mejor revisar pandas iloc vs ix vs loc explicación o Loc vs. iloc vs. ix vs. at vs. iat .