Python 3 Pandas 如何计算(按 ascending/decending 顺序计数)Dataframe 中出现的重复项

Python 3 Pandas How to Tally (count in ascending/decending order) Duplicates in a Dataframe as They Occur

在 Python3 Pandas 中有很多关于如何计算重复项的很好的例子,但我很难弄清楚 'tally' 出现的重复项。例如:数据框列有 4 个值出现,我不想将 4 作为答案,而是在新列中寻找 1、2、3、4 值。

MyCol     Tally  DecendingTally
'a'         1          4
'a'         2          3
'b'         1          2
'b'         2          1
'a'         3          2
'd'         1          1
'a'         4          1

我试过了:

df['Tally'] = df['MyCol'].count()

...这给了我正确的重复计数,但不是计数。有什么建议么?有什么办法让它下降吗?

使用GroupBy.cumcount并添加1:

df['Tally'] = df.groupby('MyCol').cumcount().add(1)
df['DecendingTally'] = df.groupby('MyCol').cumcount(ascending=False).add(1)
print (df)
  MyCol  Tally  DecendingTally
0   'a'      1               4
1   'a'      2               3
2   'b'      1               2
3   'b'      2               1
4   'a'      3               2
5   'd'      1               1
6   'a'      4               1