9

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 :)

cs95
  • 379,657
  • 97
  • 704
  • 746
oakca
  • 1,408
  • 1
  • 18
  • 40

3 Answers3

10

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)

cs95
  • 379,657
  • 97
  • 704
  • 746
6
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
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
4

I believe you need 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

For you another question is best check pandas iloc vs ix vs loc explanation or Loc vs. iloc vs. ix vs. at vs. iat.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252