小编Tom*_*mas的帖子

Python pandas在groupby和aggregate之后排序

我正在尝试在分组和聚合后对数据(Pandas)进行排序,而且我被卡住了.我的数据:

data = {'from_year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
    'name': ['John', 'John1', 'John', 'John', 'John4', 'John', 'John1', 'John6'],
    'out_days': [11, 8, 10, 15, 11, 6, 10, 4]}
persons = pd.DataFrame(data, columns=["from_year", "name", "out_days"])

days_off_yearly = persons.groupby(["from_year", "name"]).agg({"out_days": [np.sum]})

print(days_off_yearly)
Run Code Online (Sandbox Code Playgroud)

之后我将数据排序:

                out_days
                     sum
from_year name          
2010      John        17
2011      John        15
          John1       18
2012      John        10
          John4       11
          John6        4
Run Code Online (Sandbox Code Playgroud)

我想通过from_year和out_days sum对数据进行排序,并期望数据为:

                out_days
                     sum
from_year name          
2012      John4       11
          John        10
          John6        4    
2011      John1       18
          John        15 …
Run Code Online (Sandbox Code Playgroud)

python sorting grouping pandas

9
推荐指数
1
解决办法
7347
查看次数

标签 统计

grouping ×1

pandas ×1

python ×1

sorting ×1