按pandas中索引的groupby和对多索引 Dataframe 中的列进行排序

7uhlpewt  于 2023-04-28  发布在  其他
关注(0)|答案(2)|浏览(95)
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]
              }
)

我已经按属性、策略和类型对上述 Dataframe 进行了分组。

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
ruarlubt

ruarlubt1#

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
gywdnpxw

gywdnpxw2#

我觉得你只需要:

d = df.loc[slice("LS")].groupby(level="Strategy")["P&L"].sum()
df.sort_values(
    ["Attr", "Strategy"], ascending=[False, True], 
    key=lambda x: -x.map(d), inplace=True
)

输出:

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

相关问题