python - 如何按子组对 DataFrame 进行排名

标签 python pandas

如果我有一个 DataFrame 例如

  col1   col2  col3
0   x1  typeA     3
1   x2  typeB    13
2   x3  typeB     3
3   x4  typeA     5
4   x5  typeB     1
5   x6  typeA     1

是否有一种方法可以按 col3 对 col2 中的每种类型对行进行排名?例如,这个解决方案看起来像

  col1   col2  col3  rank
0   x1  typeA     3    2
1   x2  typeB    13    1
2   x3  typeB     3    2 
3   x4  typeA     5    1
4   x5  typeB     1    3
5   x6  typeA     1    3

最佳答案

transform 保持与原始数据框相同的形状。然后使用 lambda 函数根据 col2 的分组对 col3 进行排名..

df['col4'] = df.groupby('col2').col3.transform(lambda group: group.rank())

>>> df
  col1   col2  col3  col4
0   x1  typeA     3     2
1   x2  typeB    13     3
2   x3  typeB     3     2
3   x4  typeA     5     3
4   x5  typeB     1     1
5   x6  typeA     1     1

关于python - 如何按子组对 DataFrame 进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38570175/

相关文章:

python - 按整数索引 Pandas 数据帧

python - 对 Pandas 中的每周数据进行上采样

python - 将季度观察值分配给下个季度的月份

python - If 语句为假但其中的代码仍然运行以及 python 中整个脚本中的所有代码 - Python 新手

Python:我不想解压文件夹,只想将特定文件保存到输出文件夹。我没有得到预期的输出

python - 在 django 2.2 中从 data-url 获取 id 时出现问题

python - Pandas Dataframe 使用前 N 行值分配值

python - 我的 sum 函数没有对某些列的值求和

python - 使用 sympy 扩展索引符号方程

Python - 在路由器上远程登录并列出完整结果(点击空格键)