简体   繁体   English

给定总和对 multiIndex dataframe 进行排序

[英]Sort multiIndex dataframe given the sum

Hello I have prepared a MultiIndex table in Pandas that looks like this:您好,我在 Pandas 中准备了一个 MultiIndex 表,如下所示:

Lang                C++  java  python  All
Corp     Name                             
ASW      ASW        0.0   7.0     8.0   15
         Cristiano  NaN   NaN     8.0   8
         Michael    NaN   7.0     0     7
Facebook Facebook   8.0   1.0     5.0   14
         Piter      8.0   NaN     NaN    8
         Cristiano  NaN   NaN     3.0    3
         Michael    NaN   1.0     2.0    3
Google   Google     2.0  24.0     1.0   27
         Michael    NaN  24.0     NaN   24
         Piter      2.0   NaN     NaN    2
         Cristiano  NaN   NaN     1.0    1

Now I would like to Sort group of rows where sum of Corp(in column "All') is sorted decsending, then I would like to select only the two index "Corp"(and their rows) which are the largest, It should looks like:现在我想对 Corp 的总和(在“All”列中)按降序排序的行组进行排序,然后我想 select 只有两个索引“Corp”(及其行)是最大的,它应该看起来喜欢:

Lang                C++  java  python  All
Corp     Name                             
Google   Google     2.0  24.0     1.0   27
         Michael    NaN  24.0     NaN   24
         Piter      2.0   NaN     NaN    2
         Cristiano  NaN   NaN     1.0    1
ASW      ASW        0.0   7.0     8.0   15
         Cristiano  NaN   NaN     8.0   8
         Michael    NaN   7.0     0     7

Thank You!谢谢你!

IIUC, you can sort_values per group, then slice using the max sum of All per group: sort_values ,您可以对每组排序值,然后使用每组 All 的最大总和进行切片:

out = (df
    # for each company, sort the values using the All column in descending order
   .groupby(level=0).apply(lambda g: g.sort_values('All', ascending=False))
    # calculate the sum or All per company
    # get the index of the top 2 companies (nlargest(2))
    # slice to keep only those
   .loc[lambda d: d.groupby(level=0)['All'].sum().nlargest(2).index]
)

output: output:

Lang                C++  java  python  All
Corp     Name                             
Google   Google     2.0  24.0     1.0   27
         Michael    NaN  24.0     NaN   24
         Piter      2.0   NaN     NaN    2
         Cristiano  NaN   NaN     1.0    1
Facebook Facebook   8.0   1.0     5.0   14
         Piter      8.0   NaN     NaN    8
         Cristiano  NaN   NaN     3.0    3
         Michael    NaN   1.0     2.0    3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM