python - Pandas 中的多索引排序

标签 python sorting pandas multi-index

我有一个通过 groupby 操作创建的多索引 DataFrame。我正在尝试使用索引的多个级别进行复合排序,但我似乎无法找到满足我需要的排序函数。

初始数据集如下所示(各种产品的每日销售量):

         Date Manufacturer Product Name Product Launch Date  Sales
0  2013-01-01        Apple         iPod          2001-10-23     12
1  2013-01-01        Apple         iPad          2010-04-03     13
2  2013-01-01      Samsung       Galaxy          2009-04-27     14
3  2013-01-01      Samsung   Galaxy Tab          2010-09-02     15
4  2013-01-02        Apple         iPod          2001-10-23     22
5  2013-01-02        Apple         iPad          2010-04-03     17
6  2013-01-02      Samsung       Galaxy          2009-04-27     10
7  2013-01-02      Samsung   Galaxy Tab          2010-09-02      7

我使用 groupby 获取日期范围内的总和:

> grouped = df.groupby(['Manufacturer', 'Product Name', 'Product Launch Date']).sum()
                                               Sales
Manufacturer Product Name Product Launch Date       
Apple        iPad         2010-04-03              30
             iPod         2001-10-23              34
Samsung      Galaxy       2009-04-27              24
             Galaxy Tab   2010-09-02              22

到目前为止一切顺利!

现在我要做的最后一件事是按发布日期对每个制造商的产品进行排序,但将它们按层次分组在制造商下 - 这就是我要做的所有事情:

                                               Sales
Manufacturer Product Name Product Launch Date       
Apple        iPod         2001-10-23              34
             iPad         2010-04-03              30
Samsung      Galaxy       2009-04-27              24
             Galaxy Tab   2010-09-02              22

当我尝试 sortlevel() 时,我失去了以前拥有的漂亮的按公司划分的层次结构:

> grouped.sortlevel('Product Launch Date')
                                               Sales
Manufacturer Product Name Product Launch Date       
Apple        iPod         2001-10-23              34
Samsung      Galaxy       2009-04-27              24
Apple        iPad         2010-04-03              30
Samsung      Galaxy Tab   2010-09-02              22

sort() 和 sort_index() 只是失败了:

grouped.sort(['Manufacturer','Product Launch Date'])
KeyError: u'no item named Manufacturer'

grouped.sort_index(by=['Manufacturer','Product Launch Date'])
KeyError: u'no item named Manufacturer'

看似简单的操作,但我不太明白。

我并不一定要为此使用 MultiIndex,但由于这是 groupby() 返回的内容,所以这就是我一直在使用的内容。

顺便说一句,生成初始 DataFrame 的代码是:

data = {
  'Date': ['2013-01-01', '2013-01-01', '2013-01-01', '2013-01-01', '2013-01-02', '2013-01-02', '2013-01-02', '2013-01-02'],
  'Manufacturer' : ['Apple', 'Apple', 'Samsung', 'Samsung', 'Apple', 'Apple', 'Samsung', 'Samsung',],
  'Product Name' : ['iPod', 'iPad', 'Galaxy', 'Galaxy Tab', 'iPod', 'iPad', 'Galaxy', 'Galaxy Tab'], 
  'Product Launch Date' : ['2001-10-23', '2010-04-03', '2009-04-27', '2010-09-02','2001-10-23', '2010-04-03', '2009-04-27', '2010-09-02'],
  'Sales' : [12, 13, 14, 15, 22, 17, 10, 7]
}
df = DataFrame(data, columns=['Date', 'Manufacturer', 'Product Name', 'Product Launch Date', 'Sales'])

最佳答案

一个 hack 是改变级别的顺序:

In [11]: g
Out[11]:
                                               Sales
Manufacturer Product Name Product Launch Date
Apple        iPad         2010-04-03              30
             iPod         2001-10-23              34
Samsung      Galaxy       2009-04-27              24
             Galaxy Tab   2010-09-02              22

In [12]: g.index = g.index.swaplevel(1, 2)

Sortlevel,它(如您所见)按顺序对 MultiIndex 级别进行排序:

In [13]: g = g.sortlevel()

然后交换回来:

In [14]: g.index = g.index.swaplevel(1, 2)

In [15]: g
Out[15]:
                                               Sales
Manufacturer Product Name Product Launch Date
Apple        iPod         2001-10-23              34
             iPad         2010-04-03              30
Samsung      Galaxy       2009-04-27              24
             Galaxy Tab   2010-09-02              22

我认为 sortlevel 不应按顺序对剩余的标签进行排序,因此会产生 github 问题。 :) 尽管值得一提的是关于 "the need for sortedness" 的文档注释.

注意:您可以通过重新排序初始 groupby 的顺序来避免第一个 swaplevel:

g = df.groupby(['Manufacturer', 'Product Launch Date', 'Product Name']).sum()

关于python - Pandas 中的多索引排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17242970/

相关文章:

python - 多处理池管理器命名空间 EOF 错误

Python:替换双引号中的制表符

python - 寻找一种方法来组织从几天到 session 的 GPS 数据(连续数据延伸到第二天的情况)

javascript - 在javascript中手动对数组进行排序

python - 在日期时间中转换 DataFrame 列类型

python - Django REST Framework - 在 URL 中添加 2 个 PK

python - 为什么这些 numpy 操作不等效,我该如何解决这个问题?

用于排序元组的 Python 字典,可以做得更好吗?

Python/Pandas/Numpy - 直接计算两个日期之间不包括节假日的工作日数

python - 如何从包含列表的 Pandas 列中进行一次热编码?