Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting rows in a MultiIndex dataframe by index without losing any levels

I would like to select a row called 'Mid', without losing it's index 'Site'

Following code shows the 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

desired outcome would be following:

                         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

following answers give the desired outcome:

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

ty MaxU, COLDSPEED and jezrael for the answers :)

like image 894
oakca Avatar asked Dec 19 '17 11:12

oakca


1 Answers

You can also use loc with double braces.

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

In your case, I'd suppose it would be m.commodity.loc[['Mid']].

When talking about loc versus ix is that the latter is deprecated, use loc/iloc/iat/xs for indexing.

ix makes assumptions about what is passed, and accepts either labels or positions. loc is purely label based, while iloc is purely index (positional based)

like image 198
cs95 Avatar answered Sep 18 '22 07:09

cs95