熊猫分组分组



聚合是数据分析中最常见的操作之一。不同的技术为我们提供了有效组合和汇总我们感兴趣的领域(列,属性)的多种方法。本文将讨论熊猫中聚合的实现。
在我的专业领域,我很少使用python,但是我经常听到这种语言的优缺点,尤其是在处理数据时。因此,我将在此处绘制与T-SQL的并行操作并给出一些代码示例。作为数据,我将使用最受欢迎的数据集-Irises Fisher

首先想到的是获取任何虹膜参数的最大值,最小值或平均值,并按该植物的种类分组。在python中,使用大熊猫的外观如下所示:

import pandas as pd

df = pd.read_csv('iris.csv', delimiter = ',')
print(df.groupby('variety').max()[['sepalLength']].to_markdown())

结果:

| 品种| sepal.length |
|:----------- | ---------------:|
| Setosa | 5.8 |
| 杂色| 7 |
| 维珍妮卡| 7.9 |

或者:

import pandas as pd
df = pd.read_csv('iris.csv', delimiter = ',')

print(df.groupby('variety').sepalLength.agg(
    maxSepalLength  = 'max',
    minSepalLength  = 'min',
    ).to_markdown())

结果:

| 品种| maxSepalLength | minSepalLength |
|:----------- | -----------------:| ----------------- :|
| Setosa | 5.8 | 4.3 |
| 杂色| 7 | 4.9 |
| 维珍妮卡| 7.9 | 4.9 |

或使用lambda表达式:

import pandas as pd
df = pd.read_csv('iris.csv', delimiter = ',')

print(df.groupby('variety').sepalLength.agg([
    lambda x: x.max(), 
    lambda x: x.min()
    ]).to_markdown())

结果:

| 品种| <lambda_0> | <lambda_1> |
|:----------- | -------------:| -------------:| |
| Setosa | 5.8 | 4.3 |
| 杂色| 7 | 4.9 |
| 维珍妮卡| 7.9 | 4.9 |

DataFrame实例功能
to_markdown()
允许您以通常的(控制台)形式显示表(DataFrame)。

在T-SQL上,此操作如下所示:

select i.Variety, max(i.SepalLength) as maxSepalLength
    from Iris i
        group by i.Variety

结果:

Setosa 5.8
Versicolor 7.0
Virginica 7.9

但是假设现在我们想获取虹膜所有参数的最大值和最小值(如果喜欢平均值),自然地对于每种植物类型,都在这里生成了T-SQL代码:

select
	i.Variety 
	,max(i.SepalLength) as maxSepalLength 
	,min(i.SepalLength) as minSepalLength
	,max(i.SepalWidth) as maxSepalWidth
	,min(i.SepalWidth) as minSepalWidth
	,max(i.PetalLength) as maxPetalLength
	,min(i.PetalLength) as mibPetalLength
	,max(i.PetalWidth) as maxPetalWidth
	,min(i.PetalWidth) as minPetalWidth
from Iris i
	group by i.Variety

结果:

Setosa 5.8 4.3 4.4 2.3 1.9 1.0 0.6 0.1
Versicolor 7.0 4.9 3.4 2.0 5.1 3.0 1.8 1.0
Virginica 7.9 4.9 3.8 3.8 2.2 6.9 4.5 2.5 1.4

在熊猫中,仅在2019年7月18日的0.25.0版本中出现了群体聚集的可能性(之前所做的工作) ?),有几种变体,请考虑一下:

import pandas as pd
df = pd.read_csv('iris.csv', delimiter = ',')

df.groupby('variety').agg(
    maxSepalLength = pd.NamedAgg(column = 'sepalLength', aggfunc = 'max'),
    minSepalLength = pd.NamedAgg(column = 'sepalLength', aggfunc = 'min'),
    maxSepalWidth = pd.NamedAgg(column = 'sepalWidth', aggfunc = 'max'),
    minSepalWidth = pd.NamedAgg(column = 'sepalWidth', aggfunc = 'min'),
    maxPetalLength = pd.NamedAgg(column = 'petalLength', aggfunc = 'max'),
    minPetalLength = pd.NamedAgg(column = 'petalLength', aggfunc = 'min'),
    maxPetalWidth = pd.NamedAgg(column = 'petalWidth', aggfunc = 'max'),
    minPetalWidth = pd.NamedAgg(column = 'petalWidth', aggfunc = 'min'),
    )

结果:

Setosa 5.8 4.3 4.4 2.3 1.9 1.0 0.6 0.1
Versicolor 7.0 4.9 3.4 3.4 2.0 5.1 3.0 1.8 1.0
Virginica 7.9 4.9 3.8 2.2 6.9 4.5 2.5 1.4

功能
DataFrame.agg(self, func, axis=0, *args, **kwargs)

允许在给定轴上聚合多个操作。作为参数,该函数接收** kwargs(命名的参数,有关详细信息,请参见habr上的文章),该kwargs 是在其上执行操作的列,并且聚合函数的名称用单引号引起来。录音看起来很庞大。继续。

使用lambda表达式的相同解决方案看起来更加简洁明了:

import pandas as pd
df = pd.read_csv('iris.csv', delimiter = ',')

df.groupby('variety').agg([
    lambda x: x.max(),
    lambda x: x.min()
    ])

结果:

Setosa 5.8 4.3 4.4 2.3 1.9 1.0 0.6 0.1
Versicolor 7.0 4.9 3.4 3.4 5.1 5.1 1.8 1.8 1.0
Virginica 7.9 4.9 3.8 2.2 2.2 6.9 4.5 2.5 1.4

与其他语言相比,当我解决相同类型的问题时,我经常听到Python写的东西少得多。在这里,与T-SQL相比,可以同意这一点,但是诸如SQL或T-SQL之类的语言工具的表达方式的清晰度和顺序就完全消失了(个人观点)。

本文的数据集和代码可在此处找到

。0.25.0(2019年7月18日)pandas中的新增功能


All Articles