我如何“合并"在具有聚合功能的Pandas列中按相同值排列行? [英] How can I "merge" rows by same value in a column in Pandas with aggregation functions?

查看:547
本文介绍了我如何“合并"在具有聚合功能的Pandas列中按相同值排列行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据帧中的行分组(给定一列).然后,我想接收一个经过编辑的数据框,我可以为其确定哪个聚合函数有意义.默认值应该只是该组中第一个条目的值.

I would like to group rows in a dataframe, given one column. Then I would like to receive an edited dataframe for which I can decide which aggregation function makes sense. The default should be just the value of the first entry in the group.

(如果该解决方案也适用于两列的组合,那将是很好的选择)

(it would be nice if the solution also worked for a combination of two columns)

#!/usr/bin/env python

"""Test data frame grouping."""

# 3rd party modules
import pandas as pd


df = pd.DataFrame([{'id': 1, 'price': 123, 'name': 'anna', 'amount': 1},
                   {'id': 1, 'price':   7, 'name': 'anna', 'amount': 2},
                   {'id': 2, 'price':  42, 'name': 'bob', 'amount': 30},
                   {'id': 3, 'price':   1, 'name': 'charlie', 'amount': 10},
                   {'id': 3, 'price':   2, 'name': 'david', 'amount': 100}])
print(df)

提供数据框:

   amount  id     name  price
0       1   1     anna    123
1       2   1     anna      7
2      30   2      bob     42
3      10   3  charlie      1
4     100   3    david      2

我想得到:

amount  id     name  price
     3   1     anna    130
    30   2      bob     42
   110   3  charlie      3

所以:

  • id列中具有相同值的条目属于一起.完成该操作后,仍然应该有一个id列,但是它应该只有唯一的值.
  • amountprice中具有相同id的所有值都被求和
  • 对于name,仅采用第一个(按数据帧的当前顺序).
  • Entries with the same value in the id column belong together. After that operation, there should still be an id column, but it should have only unique values.
  • All values in amount and price which have the same id get summed up
  • For name, just the first one (by the current order of the dataframe) is taken.

熊猫有可能吗?

推荐答案

您正在寻找

aggregation_functions = {'price': 'sum', 'amount': 'sum', 'name': 'first'}
df_new = df.groupby(df['id']).aggregate(aggregation_functions)

给出

    price     name  amount
id                        
1     130     anna       3
2      42      bob      30
3       3  charlie     110

这篇关于我如何“合并"在具有聚合功能的Pandas列中按相同值排列行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆