什么是做内存保存列聚合的 pythonic 方法?

What is pythonic way to do memory save column aggregations?

我是 python 的新手,很抱歉这是一个愚蠢的问题。我有 table 之类的数据结构,想对每一列应用不同的聚合函数以获得某种 total 行:

data = [
   [1, 2, 3, 4, 5],
   [6, 7, 8, 9, 10],
   [11, 12, 13, 14, 15],
]
agg_func = [sum, min, max, mean, median]

total_row = [agg_func[index](value) for index, value in enumerate(zip(*data))] 

如果我有大量数据(数百万行数据),这是否是进行这种聚合的正确方法(就内存节省而言)?有什么方法可以更快地进行此类计算吗?

尝试numpy进行这种计算。它允许您在高维结构的第一维上应用聚合函数。

import numpy as np
data = np.array([
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15],
])
agg_func = [np.sum, np.min, np.max, np.mean, np.median]
total_row = [f(data, axis=0) for f in agg_func]