首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从熊猫群()组中选择列中最大值行?

如何从熊猫群()组中选择列中最大值行?
EN

Stack Overflow用户
提问于 2021-12-08 19:56:26
回答 2查看 487关注 0票数 1

我有一张这样的桌子:

代码语言:javascript
运行
复制
import pandas as pd

df = pd.DataFrame(
        [
            ['john', 'rdgsdr', 2, 'A'],
            ['ann',  'dsdfds', 3, 'A'],
            ['john', 'jkfgdj', 1, 'B'],
            ['bob',  'xcxfcd', 5, 'A'],
            ['john', 'uityuu', 3, 'C'],
            ['ann',  'werwwe', 2, 'C'],
        ],
        columns=['name', 'stuff', 'orders', 'store']
    )

# df
#    name   stuff  orders store
# 0  john  rdgsdr       2     A
# 1   ann  dsdfds       3     A
# 2  john  jkfgdj       1     B
# 3   bob  xcxfcd       5     A
# 4  john  uityuu       3     C
# 5   ann  werwwe       2     C

我需要为每个名称提取具有最大订单数的行,并为该名称计算所有商店的列表。如下所示:

代码语言:javascript
运行
复制
grouped = df.groupby('name')

for name, group in grouped:
    print('-'*5, name, '-'*5)
    print(group)

# ----- ann -----
#   name   stuff  orders store
# 1  ann  dsdfds       3     A  <- max(orders) for ann
# 5  ann  werwwe       2     C
# ----- bob -----
#   name   stuff  orders store
# 3  bob  xcxfcd       5     A  <- max(orders) for bob
# ----- john -----
#    name   stuff  orders store
# 0  john  rdgsdr       2     A
# 2  john  jkfgdj       1     B
# 4  john  uityuu       3     C  <- max(orders) for john

# ##########################
# This is what I want to get
# ##########################
>>> result
   name   stuff  max orders  all stores
1  ann   dsdfds           3         A,C
3  bob   xcxfcd           5           A
4  john  uityuu           3       A,B,C

我试过这个:

代码语言:javascript
运行
复制
result = grouped.agg(
        **{
            # 'stuff': 'stuff',
            'max orders': pd.NamedAgg('orders', max),
            'all stores': pd.NamedAgg('store', lambda s: s.str.join(',')),
        }
    )

但我不知道如何在结果中包括“资料”栏(在我真正的应用程序中,我有许多这样的额外专栏,也许还有几十篇)。此外,联接给我的是列表而不是字符串:

代码语言:javascript
运行
复制
>>> result
   name  max orders all stores
0   ann           3     [A, C]
1   bob           5          A
2  john           3  [A, B, C]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-08 20:20:19

您可以通过将this answer与groupby相结合来获得他们曾经工作过的商店列表。

代码语言:javascript
运行
复制
# Get stores that each person works at
stores_for_each_name = df.groupby('name')['store'].apply(','.join)

# Get row with largest order value for each name
df = df.sort_values('orders', ascending=False).drop_duplicates('name').rename({'orders': 'max_orders'}, axis=1)

# Replace store column with comma-separated list of stores they have worked at
df = df.drop('store', axis=1)
df = df.join(stores_for_each_name, on='name')

输出:

代码语言:javascript
运行
复制
   name   stuff  max_orders  store
3   bob  xcxfcd           5      A
1   ann  dsdfds           3    A,C
4  john  uityuu           3  A,B,C
票数 1
EN

Stack Overflow用户

发布于 2021-12-08 20:00:00

尝试使用first

代码语言:javascript
运行
复制
out = df.set_index('stuff').groupby('name').agg(stuff = ('orders' , 'idxmax'),
                                          max_orders = ('orders' , 'max'),
                                          all_stores = ('store',','.join))#.reset_index()
Out[200]: 
       stuff  max_orders all_stores
name                               
ann   dsdfds           3        A,C
bob   xcxfcd           5          A
john  uityuu           3      A,B,C
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70280986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档