基于唯一的多列索引的来自另一个 DataFrame 的新 pandas DataFrame

New pandas DataFrame from another DataFrame based on a unique multiple column index

我正在尝试根据唯一的多列索引从另一个 pandas.DataFrame 创建一个新的 pandas.DataFrame。我可以使用 df.index.drop_duplicates() 创建 pandas.core.index.MultiIndex 并获得正确的结果,但我不知道如何将其转换为 pandas.DataFrame.

以下脚本使用 SQL 查询创建原始 DataFrame。

import sqlite3 as db
import pandas as pd

conn = db.connect('C:/data.db')
query = """SELECT TimeStamp, UnderlyingSymbol, Expiry, Strike, CP, BisectIV, OTMperc FROM ActiveOptions
           WHERE TimeStamp = '2015-11-09 16:00:00' AND UnderlyingSymbol = 'INTC' AND
           Expiry < '2015-11-27 16:00:00' AND OTMperc < .02  AND OTMperc > -.02
           ORDER BY UnderlyingSymbol, Expiry, ABS(OTMperc)"""

df = pd.read_sql_query(sql=query, con=conn,index_col=['TimeStamp', 'UnderlyingSymbol', 'Expiry'],
                       parse_dates=['TimeStamp', 'Expiry'])

该脚本创建以下 DataFrame:

In[6]: df
Out[6]: 
                                                          Strike  CP  BisectIV  OTMperc
TimeStamp           UnderlyingSymbol Expiry                                            
2015-11-09 16:00:00 INTC             2015-11-13 16:00:00    33.5  -1    0.2302  -0.0045
                                     2015-11-13 16:00:00    33.5   1    0.2257   0.0045
                                     2015-11-13 16:00:00    33.0  -1    0.2442   0.0105
                                     2015-11-13 16:00:00    33.0   1    0.2426  -0.0106
                                     2015-11-13 16:00:00    34.0   1    0.2240   0.0191
                                     2015-11-13 16:00:00    34.0  -1    0.2295  -0.0195

                                     2015-11-20 16:00:00    33.5   1    0.2817   0.0045
                                     2015-11-20 16:00:00    33.5  -1    0.2840  -0.0045
                                     2015-11-20 16:00:00    33.0  -1    0.2935   0.0105
                                     2015-11-20 16:00:00    33.0   1    0.2914  -0.0106
                                     2015-11-20 16:00:00    34.0   1    0.2718   0.0191
                                     2015-11-20 16:00:00    34.0  -1    0.2784  -0.0195

创建具有唯一多列索引的新 DataFrame 会生成以下输出:

In[10]: new_df = df.index.drop_duplicates()
In[11]: new_df
Out[11]: 
MultiIndex(levels=[[2015-11-09 16:00:00], [u'INTC'], [2015-11-13 16:00:00, 2015-11-20 16:00:00]],
           labels=[[0, 0], [0, 0], [0, 1]],
           names=[u'TimeStamp', u'UnderlyingSymbol', u'Expiry'])

In[12]: type(new_df)
Out[12]: pandas.core.index.MultiIndex

有什么想法吗?

问题是您将 new_df 设置为删除了重复项的索引列表:

new_df = df.index.drop_duplicates()

您想要的是 select 只有没有重复索引的行。您可以使用 duplicated 函数来过滤您的旧数据框:

new_df = df[~df.index.duplicated()]

一个小例子,基于this:

#create data sample with multi index
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
          ['one', 'one', 'one', 'two', 'one', 'two', 'one', 'one']]
#(the first and last are duplicates)
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.Series(np.random.randn(8), index=index)

原始数据:

>>> s
first  second
bar    one      -0.932521
       one       1.969771
baz    one       1.574908
       two       0.125159
foo    one      -0.075174
       two       0.777039
qux    one      -0.992862
       one      -1.099260
dtype: float64

并过滤重复项:

>>> s[~s.index.duplicated()]
first  second
bar    one      -0.932521
baz    one       1.574908
       two       0.125159
foo    one      -0.075174
       two       0.777039
qux    one      -0.992862
dtype: float64