Python 如何从dataframe列中获取重复次数最多的行

Python 如何从dataframe列中获取重复次数最多的行,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我想从下面的数据框中获得最经常性的金额,以及it描述。数据帧的长度比我在这里显示的更长 数据帧 description amount type cosine_group 1 295302|service fee 295302|microloa 1500.0 D 24 2 1292092|rpmt microloan|71302 20000.0 D

我想从下面的数据框中获得最经常性的金额,以及it描述。数据帧的长度比我在这里显示的更长

数据帧

          description                        amount type    cosine_group
1       295302|service fee 295302|microloa  1500.0   D         24
2       1292092|rpmt microloan|71302        20000.0  D         31
3       qr q10065116702 fund trf 0002080661 30000.0  D         12
4       qr q10060597280 fund trf 0002080661 30000.0  D         12
5       1246175|service fee 1246175|microlo 3000.0   D         24
6       qr q10034118487 fund trf 0002080661 2000.0   D         12
在这里,我尝试使用grouby函数

df.groupby(['cosine\u group'])['amount'].value\u counts()[:2]

上面的代码返回

cosine_group  amount 
12            30000.0    7
              30000.0    6
       
我需要
说明
以及最经常性的
金额

预期输出为:

     description                                amount
   qr q10065116702 fund trf 0002080661         30000.0  
   qr q10060597280 fund trf 0002080661         30000.0
您可以使用以下模式:

  description  amount type
0           A           15
1           B         2000
2           C         3000
3           C         3000
4           C         3000
5           D           30
6           E           20
7           A           15

df[df['amount type'].eq(df['amount type'].mode().loc[0])]

  description  amount type
2           C         3000
3           C         3000
4           C         3000
解释:

df[mask] # will slice the dataframe based on boolean series (select the True rows) which is called a mask
df['amount type'].eq(3000) # .eq() stands for equal, it is a synonym for == in pandas
df['amount type'].mode() # is the mode of multiple values, which is defined as the most common
df['amount type'].loc[0] # returns the result with index 0, to get int instead of series

“我希望获得最经常性的金额”。那你为什么用groupby呢?因为我想我可以用
groupby
来得到发生率最高的组,然后从中提取数量和描述它似乎很好,但我想我需要对上面表达式中发生的事情进行更多的说明。上面的
eq
功能是什么expression@ChukwukaOkolie更新了我的答案,并添加了一个解释:如何确保我总是获得前三名最常出现的金额?@Chukwkaokolie,这是一个稍微不同的问题。你能不能再提出一个问题,并把链接写在这里?因为这样,未来的读者就更容易找到解决问题的正确方法。