question on groupby.agg

497 views
Skip to first unread message

Neal Becker

unread,
Nov 25, 2013, 8:34:41 AM11/25/13
to pydata
Admittedly, my use my seem odd.  I have used groupby.agg to combine results from multiple experiments, as we discussed a couple of weeks ago.  But now I have a new problem.  I have data where elements are not scalar but vectors.  So I have a dataframe where some columns are a vector (using a list or a tuple).

If I try to use groupby.agg on this, I get:

  File "./plot_stuff2.py", line 231, in combined_cols
    rdf = df.groupby(list(columns_to_group)).agg(d).reset_index()
  File "/home/nbecker/.local/lib/python2.7/site-packages/pandas/core/groupby.py", line 344, in agg
    return self.aggregate(func, *args, **kwargs)
  File "/home/nbecker/.local/lib/python2.7/site-packages/pandas/core/groupby.py", line 1727, in aggregate
    result[col] = colg.aggregate(agg_how)
  File "/home/nbecker/.local/lib/python2.7/site-packages/pandas/core/groupby.py", line 1420, in aggregate
    return self._python_agg_general(func_or_funcs, *args, **kwargs)
  File "/home/nbecker/.local/lib/python2.7/site-packages/pandas/core/groupby.py", line 476, in _python_agg_general
    result, counts = self.grouper.agg_series(obj, f)
  File "/home/nbecker/.local/lib/python2.7/site-packages/pandas/core/groupby.py", line 883, in agg_series
    return self._aggregate_series_pure_python(obj, func)
  File "/home/nbecker/.local/lib/python2.7/site-packages/pandas/core/groupby.py", line 917, in _aggregate_series_pure_python
    raise ValueError('Function does not reduce')
ValueError: Function does not reduce

I wonder if this restriction is unnecessary?  Or maybe I just shouldn't be trying to use groupby.agg for this.  Or perhaps I should be using some other approach entirely.

The purpose of this is to combine results from experiments.  The combinations would be simple functions like sum.  In this case, it would be sum element-by-element of these vectors, producing a vector result to be put into the new df.

That is, I might have a df like:

0 (1,2,3)
1 (4,5,6)

And the reduction should produce

0 (5, 7, 9)

Wouter Overmeire

unread,
Nov 26, 2013, 9:01:59 AM11/26/13
to pyd...@googlegroups.com



2013/11/25 Neal Becker <ndbe...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Does this example help?
 
In [151]: df
Out[151]: 
              A  B
0     (1, 2, 3)  0
1     (4, 5, 6)  0
2     (7, 8, 9)  1
3  (10, 11, 12)  1

In [152]: myagg = lambda s:tuple(sum(i) for i in zip(*s))                                                                                                                                                                                                                                                                      

In [153]: df.groupby('B').agg(myagg)
Out[153]: 
              A
B              
0     (5, 7, 9)
1  (17, 19, 21)

Reply all
Reply to author
Forward
0 new messages