python - 返回 Pandas 中组/多索引的前 n 个值

标签 python sorting pandas

我有一个包含每日产品和体积数据的 df:

date        product     volume
20160101    A           10
20160101    B           5
...
20160102    A           20
...
...
20160328    B           20
20160328    C           100
...
20160330    D           20

我通过以下方式按月对它进行了分组

df['yearmonth'] = df.date.astype(str).str[:6]
grouped = df.groupby(['yearmonth','product'])['Volume'].sum()

这给了我一系列的形式:

yearmonth   product 
201601      A       100
            B       90
            C       90
            D       85
            E       180
            F       50
            ...
201602      A       200
            C       120
            F       220
            G       40
            I       50
            ...
201603      B       120
            C       110
            D       110
            ...

我想返回每个产品每月的前 n 个销量值。例如,前 3 个值将返回:

201601  A  100
        B   90
        C   90
        E   180
201602  A   200
        C   120
        F   220
201603  B   120
        C   110
        D   110

我可以使用 pd.IndexSliceselect 找到一些答案,但它们似乎只对索引起作用。我不知道如何对单个组的值进行排序

最佳答案

您可以使用 SeriesGroupBy.nlargest :

print (grouped.groupby(level='yearmonth').nlargest(3).reset_index(level=0, drop=True))
yearmonth  product
201601     E          180
           A          100
           B           90
201602     F          220
           A          200
           C          120
201603     B          120
           C          110
           D          110
Name: val, dtype: int64

您也可以使用 to_datetimeto_period转换为 year-month 期间:

print (df)
        date product  Volume
0   20160101       A      10
1   20160101       B       5
2   20160101       C      10
3   20160101       D       5
4   20160102       A      20
5   20160102       A      10
6   20160102       B       5
7   20160102       C      10
8   20160102       D       5
9   20160328       A      20
10  20160328       C     100
11  20160328       B      20
12  20160328       D      20
13  20160330       D      20

grouped = df.groupby([pd.to_datetime(df.date, format='%Y%m%d').dt.to_period('M'),
                     'product'])['Volume'].sum()
print (grouped)
date     product
2016-01  A           40
         B           10
         C           20
         D           10
2016-03  A           20
         B           20
         C          100
         D           40
Name: Volume, dtype: int64

print (grouped.groupby(level='date').nlargest(3).reset_index(level=0, drop=True))
date     product
2016-01  A           40
         C           20
         B           10
2016-03  C          100
         D           40
         A           20
Name: Volume, dtype: int64

关于python - 返回 Pandas 中组/多索引的前 n 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41722785/

相关文章:

javascript - 通过使用 jQuery 重新排序元素来模拟 CSS Column 垂直内容流?

python - 如何使用另一个过滤后的数据帧更新数据帧

python - 将字符串化列表的列表转换为数据帧,同时维护索引

Python 3——找不到模块

python - 如何拟合包含具有可变极限的积分的函数?

python - 迭代索引效率子集

python - 将误差线添加到 Pandas 中的分组条形图

python - 在 RASA Core/NLU 中获取意图值

python - 在页面(2D 平面)上从上到下、从左到右对框进行排序。 (Python)

c - 选择排序字符 C