在loc搜索中保留日期

时间:2017-09-27 21:51:27

标签: python pandas

我有一个数据透视表日期和项目作为索引,价格和数量作为值。 我当然可以循环每个日期的交易,并使用以下方式对它们做一些事情:

df

                           price  quantities
date           product                    
2016-01-01     apple        10.0    60.0
               orange       10.0     0.0
2017-01-01     apple        10.0    60.0
               orange       10.0     0.0

dates = df.index.levels[0]

for date in dates:
  productlists.append(df.loc[date])

结果:

         price  quantities
product                    
apple     10.0    60.0
orange    10.0     0.0

         price  quantities
product                   
apple     10.0    60.0
orange    10.0     0.0

预期:

                           price  quantities
date           product                    
2016-01-01     apple        10.0    60.0
               orange       10.0     0.0
                            price  quantities
date           product 
2017-01-01     apple        10.0    60.0
               orange       10.0     0.0

现在的问题是我在产品列表中丢失了日期索引。我该怎么办?

编辑:提供我对结果的期望。

1 个答案:

答案 0 :(得分:0)

df.groupby(...)返回一个迭代器,它产生组密钥,每个组作为子数据框架。因此,您可以使用

productlists = [grp for key, grp in df.groupby(level=0)]

例如,

import pandas as pd

df = pd.DataFrame({'date': ['2016-01-01', '2016-01-01', '2017-01-01', '2017-01-01'],
 'price': [10.0, 10.0, 10.0, 10.0],
 'product': ['apple', 'orange', 'apple', 'orange'],
 'quantities': [60.0, 0.0, 60.0, 0.0]})
df = df.set_index(['date', 'product'])
productlists = [grp for key, grp in df.groupby(level=0)]
for grp in productlists:
    print(grp)

打印

                    price  quantities
date       product                   
2016-01-01 apple     10.0        60.0
           orange    10.0         0.0
                    price  quantities
date       product                   
2017-01-01 apple     10.0        60.0
           orange    10.0         0.0