如何按两个不同的dataFrame列进行排序?

时间:2014-05-20 22:40:19

标签: python pandas

我正在尝试按此dataFrame的两列排序,但我尝试过的方法还没有解决。这可能是一个非常基本的答案。任何帮助表示赞赏。

import pandas
from pandas import DataFrame, Series

data = [
('product_a','1/31/2014',53)
,('product_b','1/31/2014',44)
,('product_c','1/31/2014',36)
,('product_a','2/28/2014',52)
,('product_b','2/28/2014',43)
,('product_c','2/28/2014',35)
,('product_a','3/31/2014',50)
,('product_b','3/31/2014',41)
,('product_c','3/31/2014',34) 
]

product_df = DataFrame( data, columns=['product_desc','activity_month','prod_count'] )

product_df['pct_ch'] = product_df.groupby('product_desc')['prod_count'].pct_change()

product_df = product_df.sort('activity_month','product_desc')

结果如下:

  product_desc activity_month  prod_count    pct_ch
6     product_a      3/31/2014         50 -0.038462
3     product_a      2/28/2014         52 -0.018868
0     product_a      1/31/2014         53       NaN
4     product_b      2/28/2014         43 -0.022727
7     product_b      3/31/2014         41 -0.046512
1     product_b      1/31/2014         44       NaN
2     product_c      1/31/2014         36       NaN
8     product_c      3/31/2014         34 -0.028571
5     product_c      2/28/2014         35 -0.027778

我想要的是:

  product_desc activity_month  prod_count    pct_ch
0     product_a      1/31/2014         53       NaN
3     product_a      2/28/2014         52 -0.018868
6     product_a      3/31/2014         50 -0.038462
1     product_b      1/31/2014         44       NaN
4     product_b      2/28/2014         43 -0.022727
7     product_b      3/31/2014         41 -0.046512
2     product_c      1/31/2014         36       NaN
5     product_c      2/28/2014         35 -0.027778
8     product_c      3/31/2014         34 -0.028571

我应该如何区别对待这种情况?

更新 我将activity_month更改为日期并更新了我的排序逻辑,如下所示仍然得到没有正确输出的结果。

product_df = product_df.sort(['activity_month','product_desc'])

  product_desc     activity_month  prod_count    pct_ch
0     product_a     2014-01-31         53       NaN
1     product_b     2014-01-31         44       NaN
2     product_c     2014-01-31         36       NaN
3     product_a     2014-02-28         52 -0.018868
4     product_b     2014-02-28         43 -0.022727
5     product_c     2014-02-28         35 -0.027778
6     product_a     2014-03-31         50 -0.038462
7     product_b     2014-03-31         41 -0.046512
8     product_c     2014-03-31         34 -0.028571

1 个答案:

答案 0 :(得分:1)

在多列上进行排序时,sort()需要列的列表(因此将列名称包装在[]中)并且顺序很重要。您希望将'product_desc'作为第一个变量,将activity_month作为排序中的第二个变量:

>>> product_df.sort(['product_desc','activity_month'])

   product_desc activity_month  prod_count    pct_ch
0    product_a     2014-01-01          53       NaN 
3    product_a     2014-02-01          52 -0.018868
6    product_a     2014-03-01          50 -0.038462
1    product_b     2014-01-01          44       NaN
4    product_b     2014-02-01          43 -0.022727
7    product_b     2014-03-01          41 -0.046512
2    product_c     2014-01-01          36       NaN
5    product_c     2014-02-01          35 -0.027778
8    product_c     2014-03-01          34 -0.028571