我有一个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
... ... ... ... ... ...
我尝试了各种通过index = 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
分组。 最佳答案
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