如何通过二级采用MultiIndex DataFrame

时间:2017-01-28 06:59:34

标签: python sorting pandas dataframe multi-index

我有DataFrame MultiIndex。索引字段为OptionSymbol(级别0)和QuoteDatetime(级别1)。我已对DataFrame进行索引和排序,如下所示:

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

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

这导致以下结果:

                                      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
  ...                     ...        ...     ...        ...        ...

正如预期的那样,首先按OptionSymbol的降序排序,然后在OptionSymbol组中按升序排序

我需要做的是现在使用QuoteDatetime中的第一个值,因此结果如下所示:

                                      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
  ...                     ...        ...     ...        ...        ...

我尝试过各种各种方法来索引= 1然后我失去了OptionSymbol组。我怎么做这种呢?

使用代码进行编辑以重新创建

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

在这种情况下使用df.sort_index(level=1)的奇怪工作可以解决我的完整数据集(20多列),但我失去了OptionSymbol分组。

1 个答案:

答案 0 :(得分:2)

IIUC您只需按第二级排序索引:

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