python - Pandas groupby object.aggregate 具有自定义列表操作功能

标签 python python-3.x numpy pandas

我有一个如下所示的 csv 文件

Hour,L,Dr,Tag,Code,Vge
0,L5,XI,PS,4R,15
0,L3,St,sst,4R,17
5,L5,XI,PS,4R,12
2,L0,St,v2T,4R,11
8,L2,TI,sst,4R,8
12,L5,XI,PS,4R,18
2,L2,St,PS,4R,9
12,L3,XI,sst,4R,16

我在我的 ipython 笔记本中执行以下脚本。

In[1]
    import pandas as pd
In[2]
    df = pd.read_csv('/python/concepts/pandas/in.csv')
In[3]    
    df.head(n=9)

Out[1]: 

       Hour   L  Dr  Tag Code  Vge
    0     0  L5  XI   PS   4R   15
    1     0  L3  St  sst   4R   17
    2     5  L5  XI   PS   4R   12
    3     2  L0  St  v2T   4R   11
    4     8  L2  TI  sst   4R    8
    5    12  L5  XI   PS   4R   18
    6     2  L2  St   PS   4R    9
    7    12  L3  XI  sst   4R   16

In[4]
    df.groupby(('Hour'))['Vge'].aggregate(np.sum)



Out[2]:  
     Hour
        0     32
        2     20
        5     12
        8      8
        12    34
        Name: Vge, dtype: int64

现在我编写一个列表操作square_list

In[4]    

    newlist = []
In[5]    
    def square_list(x):
        for item in x:
            newlist.append(item**item)
        return newlist

In [44]: df.groupby(('Hour'))['Vge'].aggregate(square_list)
Out[44]: 
Hour
0     [437893890380859375, -2863221430593058543, 437...
2     [437893890380859375, -2863221430593058543, 437...
5     [437893890380859375, -2863221430593058543, 437...
8     [437893890380859375, -2863221430593058543, 437...
12    [437893890380859375, -2863221430593058543, 437...
Name: Vge, dtype: object

输出看起来很奇怪。我所期待的只是第一个输出中项目的方 block

如果我使用

df.groupby(('Hour'))['Vge'].aggregate(lambda x: x ** x)

我收到以下错误。

ValueError                                Traceback (most recent call last)
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in agg_series(self, obj, func)
   1632         try:
-> 1633             return self._aggregate_series_fast(obj, func)
   1634         except Exception:

/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in _aggregate_series_fast(self, obj, func)
   1651                                     dummy)
-> 1652         result, counts = grouper.get_result()
   1653         return result, counts

pandas/src/reduce.pyx in pandas.lib.SeriesGrouper.get_result (pandas/lib.c:38634)()

pandas/src/reduce.pyx in pandas.lib.SeriesGrouper.get_result (pandas/lib.c:38503)()

pandas/src/reduce.pyx in pandas.lib._get_result_array (pandas/lib.c:32023)()

ValueError: function does not reduce

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in aggregate(self, func_or_funcs, *args, **kwargs)
   2339             try:
-> 2340                 return self._python_agg_general(func_or_funcs, *args, **kwargs)
   2341             except Exception:

/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in _python_agg_general(self, func, *args, **kwargs)
   1167             try:
-> 1168                 result, counts = self.grouper.agg_series(obj, f)
   1169                 output[name] = self._try_cast(result, obj)

/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in agg_series(self, obj, func)
   1634         except Exception:
-> 1635             return self._aggregate_series_pure_python(obj, func)
   1636 

/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in _aggregate_series_pure_python(self, obj, func)
   1668                         isinstance(res, list)):
-> 1669                     raise ValueError('Function does not reduce')
   1670                 result = np.empty(ngroups, dtype='O')

ValueError: Function does not reduce

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-47-874cf4c23d53> in <module>()
----> 1 df.groupby(('Hour'))['Vge'].aggregate(lambda x : x**x)

/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in aggregate(self, func_or_funcs, *args, **kwargs)
   2340                 return self._python_agg_general(func_or_funcs, *args, **kwargs)
   2341             except Exception:
-> 2342                 result = self._aggregate_named(func_or_funcs, *args, **kwargs)
   2343 
   2344             index = Index(sorted(result), name=self.grouper.names[0])

/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in _aggregate_named(self, func, *args, **kwargs)
   2429             output = func(group, *args, **kwargs)
   2430             if isinstance(output, (Series, Index, np.ndarray)):
-> 2431                 raise Exception('Must produce aggregated value')
   2432             result[name] = self._try_cast(output, group)
   2433 

Exception: Must produce aggregated value

最佳答案

您仔细阅读错误了吗?它说功能没有减少。请花几分钟正确定义您想要的内容。这也是您的 square_list() 函数的确切问题,它返回一个列表,而不是列表元素的总和。它不会减少。

  1. 如果您想要简单的总和:

    df.groupby('Hour')['Vge'].sum()
    
  2. 如果您想对列中的所有元素进行平方:

    df['Vge_squared'] = df['Vge']**2
    
  3. 如果您想要组平方和:

    df.groupby('Hour')['Vge_squared'].sum()
    

或者,

def square_list(x):
    x = numpy.array(x)
    return numpy.sum(numpy.multiply(x,x))

df.groupby('Hour')['Vge'].aggregate(square_list)

或者,

def square_list(x):
    for item in x:
        newlist.append(item**item)
    return newlist

df.groupby('Hour')['Vge'].aggregate(square_list).apply(sum)

希望这有帮助。

关于python - Pandas groupby object.aggregate 具有自定义列表操作功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34114304/

相关文章:

python - 有条件地为路由设置 FastAPI 响应模型

python - 使 QGraphicsItem 只能在一个图形 View 中选择

python - 在字典python中找到前k个最大的键

python - 在不知道键/值的情况下对字典列表进行排序

python - 属性错误: list has no attribute dot

python - 基数为 10 的 int() 无效文字 : 'on' Python-Django

python - 调用同一类的另一个父级的方法

Python 3.x : Avoid overwriting of same methods from different inherited classes

python - 寻找直线和轮廓之间的交点

python - Numpy 屏蔽操作