pandas group by year, rank by sales column, in a dataframe with duplicate data(Pandas 按年份分组,按销售列排名,在具有重复数据的数据框中)
问题描述
我想创建一个年度排名(所以在 2012 年,经理 B 为 1.在 2011 年,经理 B 再次为 1).我在 pandas rank 函数上苦苦挣扎了一段时间,不想诉诸 for 循环.
I would like to create a rank on year (so in year 2012, Manager B is 1. In 2011, Manager B is 1 again). I struggled with the pandas rank function for awhile and DO NOT want to resort to a for loop.
<小时>
我遇到的问题是附加代码(之前认为这无关紧要):
The issue I'm having is with the additional code (didn't think this would be relevant before):
有什么想法吗?
这是我正在使用的真实数据结构.重新索引时遇到问题..
Any ideas?
This is the real data structure I am using.
Been having trouble re-indexing..
推荐答案
听起来你想按Year
分组,然后按降序排列Returns
.
It sounds like you want to group by the Year
, then rank the Returns
in descending order.
产量
<小时>
解决 OP 修改后的问题:错误消息
To address the OP's revised question: The error message
在尝试对索引中具有重复值的 DataFrame 进行 groupby/rank
时发生.您可以通过构造 s
在追加后具有唯一索引值来避免该问题:
occurs when trying to groupby/rank
on a DataFrame with duplicate values in the index. You can avoid the problem by constructing s
to have unique index values after appending:
产量
<小时>
如果您已经使用
If you've already appended new rows using
然后使用 reset_index
创建唯一索引:
then use reset_index
to create a unique index:
这篇关于Pandas 按年份分组,按销售列排名,在具有重复数据的数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!