Pandas 对多组数据应用多个函数
在本文中,我们将介绍如何使用Pandas对多个组应用多个函数,并且使用groupby列来进行分组。
阅读更多:Pandas 教程
准备数据
在开始之前,我们需要准备一些数据来演示我们的代码。我们将使用一个包含以下列的DataFrame:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'C': np.random.randn(8),
'D': np.random.randn(8)
})
这是我们将用来执行代码示例的DataFrame。 它将有两列分组依据 A 和B 以及两列数值 C 和D。
视图
首先,我们可以使用groupby聚合数据并应用多个函数:
grouped = df.groupby(['A', 'B']).agg({'C': [np.sum, np.mean], 'D': [np.min, np.max]})
这将计算每个分组的 C 和D 列的 sum、mean、min 和 max。 我们传递了一个字典,其中键是要分组的列名,值是要应用的函数。 在这种情况下,我们将应用四个函数:sum、mean、min 和 max。
结果如下:
C D
sum mean amin amax
A B
bar one 1.799231 0.899616 -0.350432 0.753766
three 1.809174 0.904587 -0.951654 0.631808
two -0.827797 -0.413898 -0.915300 -0.596474
foo one -0.720321 -0.360161 -0.577646 1.305872
three 1.258078 1.258078 -0.321507 -0.321507
two -1.460497 -0.730249 -1.459996 0.031295
结果是一个层次化的DataFrame, 其中第一列级别是分组列 A 和B,下一列级别是应用的函数,即C和D列的sum,mean,amin和amax。
我们可以使用列索引来选择单个函数的结果:
print(grouped['C', 'mean'])
输出如下:
A B
bar one 0.899616
three 0.904587
two -0.413898
foo one -0.360161
three 1.258078
two -0.730249
Name: (C, mean), dtype: float64
筛选列
我们可以使用列筛选器进行更进一步的筛选。比如,选择 A 列为“foo”的结果:
grouped.loc['foo']
输出如下:
C D
sum mean amin amax amin amax
B
one -0.720321 -0.360161 -0.577646 1.305872 -0.76813 1.305872
three 1.258078 1.258078 -0.321507 -0.321507 -0.321507 -0.321507
two -1.460497 -0.730249 -1.459996 0.031295 -0.04519 0.031295
其他聚合函数
还有许多其他的聚合函数可以使用。例如,可以使用自定义函数:
def my_func(x):
return x.max() - x.min()
grouped = df.groupby(['A', 'B']).agg({'C': [np.sum, np.mean], 'D': [np.min, np.max, my_func]})
print(grouped)
输出如下:
C D
sum mean amin amax my_func
A B
bar one -0.654204 -0.327102 -1.360695 0.861685 2.222380
three -0.512851 -0.256426 -1.102678 0.590424 1.693102
two -0.742809 -0.371404 -0.752443 0.176631 0.549074
foo one -1.441738 -0.720869 -2.595621 0.903545 3.498166
three 1.314550 1.314550 -0.867023 -0.867023 0.000000
two -0.192542 -0.096271 -1.097157 1.409114 2.506272
使用自定义函数计算了每个组的 C 和D 列的 my_func 这个函数值。
多个数据分组键
在前面的示例中,我们只使用了一个分组键。但是,我们经常需要将多个键一起使用进行分组。在这种情况下,只需将多个列放入列表中即可:
grouped_multi = df.groupby(['A', 'B']).agg({'C': [np.sum, np.mean], 'D': [np.min, np.max]})
print(grouped_multi)
这将创建具有多个列级别的分组数据:
C D
sum mean amin amax
A B
bar one 1.030687 0.515344 -0.438292 1.884638
three 0.501003 0.250502 -1.763097 1.051057
two -0.489946 -0.244973 -0.383742 -0.106204
foo one -0.738822 -0.369411 -0.255659 1.919374
three -0.355801 -0.355801 -0.733882 0.022639
two 2.041148 1.020574 -0.994723 1.312570
在这种情况下,我们可以通过访问两列之间的级别来选择数据:
print(grouped_multi['C', 'sum'])
输出结果如下:
A B
bar one 1.030687
three 0.501003
two -0.489946
foo one -0.738822
three -0.355801
two 2.041148
Name: (C, sum), dtype: float64
我们还可以使用具有两个分组键的 loc 来选择数据。例如,选择 A = “bar”和 B =“one” 的值:
print(grouped_multi.loc[('bar', 'one')])
输出如下:
C sum 1.030687
mean 0.515344
D amin -0.438292
amax 1.884638
Name: (bar, one), dtype: float64
总结
在本文中,我们已经学习了如何使用 Pandas 套件处理多个组,同时将多个函数应用于列组,使您能够更好地分析您的数据和提取准确的信息。无论您正在处理成千上万行的数据还是几百行的数据,这些功能都可以帮助您更轻松地进行分析。
极客教程