按 pandas 中索引的分组总和对多索引数据框中的列进行排序

问题描述 投票:0回答:1
df = pd.DataFrame({'Strategy': ['AAPL', 'AAPL', 'MSFT', 'MSFT', 'GOOGL','GOOGL', 'SPY'],
               'Attr':['LS', 'LS', 'LS', 'LS', 'LS', 'LS', 'MH'],
               'Type': ['Equity', 'Swap', 'Equity', 'Put Option', 'Equity', 'Forward', 'Equity'],
               'P&L': [2131, -304, -988, -10, 91710, -80, 10000],
               'MV': [90000, -1000, 981231, -4000, 128123, -89, -900000]
              }
)

我已经按 Attr、Strategy 和 Type 对上述数据框进行了分组。

df.groupby(['Attr', 'Strategy', 'Type'])['P&L', 'MV'].sum()
                                     P&L    MV
Attr    Strategy    Type        
LS          AAPL     Equity         2131    90000
                     Swap           -304    -1000
            GOOGL    Equity         91710   128123
                     Forward        -80     -89
            MSFT     Equity         -988    981231
                     Put Option     -10     -4000
MH          SPY      Equity         10000   -900000


我如何按以下索引对上面 LS 部分的策略进行排序:

df.loc[df['Attr'] == 'LS'].groupby(['Strategy'])['P&L'].sum().sort_values(ascending = False)
Strategy
GOOGL    91630
AAPL      1827
MSFT      -998
python pandas sorting multi-index
1个回答
0
投票

IIUC,你想要这个吗:

df_sum.assign(sortkey=df_sum.groupby(level=[0,1])['P&L']\
      .transform('sum'))\
      .sort_values(['Attr','sortkey'], ascending=[True, False])\
      .drop('sortkey', axis=1)

输出:

                            P&L      MV
Attr Strategy Type                     
LS   GOOGL    Equity      91710  128123
              Forward       -80     -89
     AAPL     Equity       2131   90000
              Swap         -304   -1000
     MSFT     Equity       -988  981231
              Put Option    -10   -4000
MH   SPY      Equity      10000 -900000
© www.soinside.com 2019 - 2024. All rights reserved.