相关疑难解决方法(0)

对列进行排序并在每个组中选择前n行pandas数据帧

我有这样的数据帧:

mainid  pidx    pidy   score
  1      a        b      2
  1      a        c      5
  1      c        a      7
  1      c        b      2
  1      a        e      8
  2      x        y      1
  2      y        z      3
  2      z        y      5
  2      x        w      12
  2      x        v      1
  2      y        x      6   
Run Code Online (Sandbox Code Playgroud)

我想groupby on column 'pidx' ,然后sort score in descending order in each group即为每个pidx

然后select head(2)是每组的前2名.

我要找的结果是这样的:

mainid   pidx    pidy    score
  1        a      e        8
  1        a      c …
Run Code Online (Sandbox Code Playgroud)

python pandas

18
推荐指数
1
解决办法
2万
查看次数

标签 统计

pandas ×1

python ×1