简体   繁体   中英

How to resort a MultiIndex DataFrame by second level

I have a DataFrame with a MultiIndex . The index fields are OptionSymbol (level 0) and QuoteDatetime (level 1). I have indexed and sorted the DataFrame like so:

sorted = df.sort_values(
    ['OptionSymbol', 'QuoteDatetime'], 
    ascending=[False, True]
)

indexed = sorted.set_index(
    ['OptionSymbol', 'QuoteDatetime'],
    drop=True
)

This results in the following:

                                      Id  Strike Expiration OptionType
OptionSymbol       QuoteDatetime                                      
ZBYMZ              2013-09-02     234669   170.0 2011-01-22        put
                   2013-09-03     234901   170.0 2011-01-22        put
                   2013-09-04     235133   170.0 2011-01-22        put
  ...                     ...        ...     ...        ...        ...
YBWNA              2010-02-12     262202    95.0 2010-02-20       call
                   2010-02-16     262454    95.0 2010-02-20       call
                   2010-02-17     262707    95.0 2010-02-20       call
  ...                     ...        ...     ...        ...        ...
XWNAX              2012-07-12     262201    90.0 2010-02-20       call
                   2012-07-16     262453    90.0 2010-02-20       call
                   2012-07-17     262706    90.0 2010-02-20       call
  ...                     ...        ...     ...        ...        ...
WWWAX              2012-04-12     262201    90.0 2010-02-20       call
                   2012-04-16     262453    90.0 2010-02-20       call
                   2012-04-17     262706    90.0 2010-02-20       call
  ...                     ...        ...     ...        ...        ...

As expected the frame is first sorted by in descending order by OptionSymbol and ascending order within the OptionSymbol group .

What I need to do is resort now by the first value in QuoteDatetime so the result looks like this:

                                      Id  Strike Expiration OptionType
OptionSymbol       QuoteDatetime                                      
XBWNA              2010-02-12     262202    95.0 2010-02-20       call
                   2010-02-16     262454    95.0 2010-02-20       call
                   2010-02-17     262707    95.0 2010-02-20       call
  ...                     ...        ...     ...        ...        ...
NWWAX              2012-04-12     262201    90.0 2010-02-20       call
                   2012-04-16     262453    90.0 2010-02-20       call
                   2012-04-17     262706    90.0 2010-02-20       call
  ...                     ...        ...     ...        ...        ...
BWNAX              2012-07-12     262201    90.0 2010-02-20       call
                   2012-07-16     262453    90.0 2010-02-20       call
                   2012-07-17     262706    90.0 2010-02-20       call
  ...                     ...        ...     ...        ...        ...
XBYMZ              2013-09-02     234669   170.0 2011-01-22        put
                   2013-09-03     234901   170.0 2011-01-22        put
                   2013-09-04     235133   170.0 2011-01-22        put
  ...                     ...        ...     ...        ...        ...

I've tried various ways of resorting by index=1 but then I lose the OptionSymbol group. How can I do this sort?

Edit with code to recreate

from collections import OrderedDict
df = OrderedDict((
    ('OptionSymbol', pd.Series(['ZBYMZ', 'ZBYMZ', 'ZBYMZ', 'YBWNA', 'YBWNA', 'YBWNA', 'XWNAX', 'XWNAX', 'XWNAX', 'WWWAX', 'WWWAX', 'WWWAX', ])),
    ('QuoteDatetime', pd.Series(['2013-09-02', '2013-09-03', '2013-09-04', '2010-02-12', '2010-02-16', '2010-02-17', '2012-07-12', '2012-07-16', '2012-07-17', '2012-04-12', '2012-04-16', '2012-04-17'])),
    ('Id', pd.Series(np.random.randn(12,))),
    ('Strike', pd.Series(np.random.randn(12,))),
    ('Expiration', pd.Series(np.random.randn(12,))),
    ('OptionType', pd.Series(np.random.randn(12,)))
))

Bizarre in this case using df.sort_index(level=1) does the trick however on my full data set (20+ columns) I lose the OptionSymbol grouping.

IIUC you can simply sort index by the second level:

In [27]: df.sort_index(level=1)
Out[27]:
                                Id  Strike  Expiration OptionType
OptionSymbol QuoteDatetime
YBWNA        2010-02-12     262202    95.0  2010-02-20       call
             2010-02-16     262454    95.0  2010-02-20       call
             2010-02-17     262707    95.0  2010-02-20       call
WWWAX        2012-04-12     262201    90.0  2010-02-20       call
             2012-04-16     262453    90.0  2010-02-20       call
             2012-04-17     262706    90.0  2010-02-20       call
XWNAX        2012-07-12     262201    90.0  2010-02-20       call
             2012-07-16     262453    90.0  2010-02-20       call
             2012-07-17     262706    90.0  2010-02-20       call
ZBYMZ        2013-09-02     234669   170.0  2011-01-22        put
             2013-09-03     234901   170.0  2011-01-22        put
             2013-09-04     235133   170.0  2011-01-22        put

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM