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

独自空忆成欢 提交于 2019-11-28 04:41:19

问题


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


回答1:


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)




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/47886401/selecting-rows-in-a-multiindex-dataframe-by-index-without-losing-any-levels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!