pandas 集合内的计算 [英] Calculations within pandas aggregate

查看:94
本文介绍了 pandas 集合内的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在pandas聚合中执行计算.我希望将计算包括在聚合中.我正在尝试的代码如下.我还在df上使用pandas软件包.

I am trying to perform a calculation within pandas aggregations. I want the calculations to be included in the aggregations. The code on what I am attempting is below. I am also using the pandas package for the df.

data = data.groupby(['type', 'name']).agg({'values': [np.min, np.max, 100 * sum([('values' > 3200)] / [np.size])]})

我要计算的公式如下:

100 * sum((values > 3200) / (np.size))

此处np是聚合的大小(聚合的数量限制为大于3200的数字).如何在聚合中执行这样的计算会很有帮助.

This is where np is the size of the aggregation (the numbers aggregated are limited to numbers > 3200). How to perform calculations like this within the aggregations would be of great help.

示例输入数据(实际数据集要大得多).重复值归因于聚合.

Example input data (actual dataset is much larger). The repeat values are due to the aggregation.

type, name, values
apple, blue, 2500
orange, green, 2800
peach, black, 3300
lemon, white, 3500

所需的示例输出(由于我尚未能够执行计算,因此数字不正确)

Desired example output (numbers are not correct due to the fact that I have yet to be able to perform the calculation):

type, name, values, np.min, np.max, calcuation
apple, blue, 2500, 1200, 40000, 2300
orange, green, 2800, 1200, 5000, 2500

推荐答案

使用df.agg字典来指定输出列的名称,这里您实际上是在编写一个试图使用三个公式的聚合函数一个命名列,而该列已经在您的数据框中,因此它将失败.

Passing df.agg a dictionary is used to specify the name of the output columns, here you're essentially writing an aggregation function which is attempting to use three formulas for one named column, and that column is already in your dataframe so its going to fail.

您应该做的事情应该看起来像这样:

What you should be doing should look more like:

data = data.groupby(['type', 'name']).agg({'min':np.min, 'max':np.max, 'calculation': calculation})

根据所需的操作方式,将计算函数重写为lambda或自定义函数.

Where you've rewritten your calculation function as either a lambda or a custom function, depending on how you want to do things.

这篇关于 pandas 集合内的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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