pandas获取groupby分组里最大值所在的行方法

作者:Tobin's Blog 时间:2021-08-14 21:39:14 

pandas获取groupby分组里最大值所在的行方法

如下面这个DataFrame,按照Mt分组,取出Count最大的那行


import pandas as pd
df = pd.DataFrame({'Sp':['a','b','c','d','e','f'], 'Mt':['s1', 's1', 's2','s2','s2','s3'], 'Value':[1,2,3,4,5,6], 'Count':[3,2,5,10,10,6]})

df


CountMtSpValue
03s1a1
12s1b2
25s2c3
310s2d4
410s2e5
56s3f6

方法1:在分组中过滤出Count最大的行


df.groupby('Mt').apply(lambda t: t[t.Count==t.Count.max()])



CountMtSpValue
Mt




s103s1a1
s2310s2d4
410s2e5
s356s3f6

方法2:用transform获取原dataframe的index,然后过滤出需要的行


print df.groupby(['Mt'])['Count'].agg(max)

idx=df.groupby(['Mt'])['Count'].transform(max)
print idx
idx1 = idx == df['Count']
print idx1

df[idx1]

Mt
s1 3
s2 10
s3 6
Name: Count, dtype: int64
0 3
1 3
2 10
3 10
4 10
5 6
dtype: int64
0 True
1 False
2 False
3 True
4 True
5 True
dtype: bool


CountMtSpValue
03s1a1
310s2d4
410s2e5
56s3f6

上面的方法都有个问题是3、4行的值都是最大值,这样返回了多行,如果只要返回一行呢?

方法3:idmax(旧版本pandas是argmax)


idx = df.groupby('Mt')['Count'].idxmax()
print idx

df.iloc[idx]
Mt
s1 0
s2 3
s3 5
Name: Count, dtype: int64


CountMtSpValue
03s1a1
310s2d4
56s3f6


df.iloc[df.groupby(['Mt']).apply(lambda x: x['Count'].idxmax())]


CountMtSpValue
03s1a1
310s2d4
56s3f6


def using_apply(df):
return (df.groupby('Mt').apply(lambda subf: subf['Value'][subf['Count'].idxmax()]))

def using_idxmax_loc(df):
idx = df.groupby('Mt')['Count'].idxmax()
return df.loc[idx, ['Mt', 'Value']]

print using_apply(df)

using_idxmax_loc(df)

Mt
s1 1
s2 4
s3 6
dtype: int64


MtValue
0s11
3s24
5s36

方法4:先排好序,然后每组取第一个


df.sort('Count', ascending=False).groupby('Mt', as_index=False).first()


MtCountSpValue
0s13a1
1s210d4
2s36f6

那问题又来了,如果不是要取出最大值所在的行,比如要中间值所在的那行呢?

思路还是类似,可能具体写法上要做一些修改,比如方法1和2要修改max算法,方法3要自己实现一个返回index的方法。 不管怎样,groupby之后,每个分组都是一个dataframe。

来源:http://www.guoguoday.com/post/pandas%E8%8E%B7%E5%8F%96groupby%E5%88%86%E7%BB%84%E9%87%8C%E6%9C%80%E5%A4%A7%E5%80%BC%E6%89%80%E5%9C%A8%E7%9A%84%E8%A1%8C/

标签:pandas,行,最大值,groupby
0
投稿

猜你喜欢

  • Python+Selenium实现短视频自动上传与发布的实践

    2021-06-12 15:58:46
  • python async with和async for的使用

    2021-10-16 16:31:02
  • PHP常用字符串函数小结(推荐)

    2023-06-14 00:18:50
  • 基于tensorflow指定GPU运行及GPU资源分配的几种方式小结

    2021-10-03 10:22:27
  • Golang高性能持久化解决方案BoltDB数据库介绍

    2024-01-27 00:13:37
  • flash(swf)遮住网页内容div的解决

    2007-10-31 07:29:00
  • Python中logging日志模块代码调试过程详解

    2021-03-13 05:40:32
  • 分享Sql Server 存储过程使用方法

    2024-01-13 06:42:36
  • 讲解数据库加密技术的功能特性与实现方法

    2008-12-18 14:24:00
  • Python操作MySQL数据库的示例代码

    2024-01-29 03:55:09
  • Python中的groupby分组功能的实例代码

    2021-09-17 20:48:15
  • CI操作cookie的方法分析(基于helper类库)

    2023-11-20 21:59:07
  • Python库skimage绘制二值图像代码实例

    2023-06-12 15:05:33
  • javascript结合canvas实现图片旋转效果

    2023-08-07 23:47:59
  • Python 通过监听端口实现唯一脚本运行方式

    2022-02-04 07:40:44
  • python 5个顶级异步框架推荐

    2021-12-23 06:21:47
  • Python实现GPU加速的基本操作

    2021-07-30 05:10:34
  • js实现GIF动图分解成多帧图片上传

    2023-08-15 00:06:38
  • python中flatten()参数示例详解

    2022-07-03 11:19:57
  • Oracle如何批量将表中字段名全转换为大写(利用简单存储过程)

    2024-01-22 11:23:41
  • asp之家 网络编程 m.aspxhome.com