Advertisement
Guest User

Untitled

a guest
Feb 6th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. In [359]: df = pandas.DataFrame({'x': 3 * ['a'] + 2 * ['b'], 'y': np.random.normal(size=5), 'z': np.random.normal(size=5)})
  2.  
  3. In [360]: df
  4. Out[360]:
  5. x y z
  6. 0 a 0.201980 -0.470388
  7. 1 a 0.190846 -2.089032
  8. 2 a -1.131010 0.227859
  9. 3 b -0.263865 -1.906575
  10. 4 b -1.335956 -0.722087
  11.  
  12. In [361]: df.groupby('x').apply(lambda x: pandas.DataFrame({'r': (x.y + x.z).sum() / x.z.sum(), 's': (x.y + x.z ** 2).sum() / x.z.sum()}))
  13. ---------------------------------------------------------------------------
  14. ValueError Traceback (most recent call last)
  15. /home/emarkley/work/src/partner_analysis2/main.py in <module>()
  16. ----> 1 df.groupby('x').apply(lambda x: pandas.DataFrame({'r': (x.y + x.z).sum() / x.z.sum(), 's': (x.y + x.z ** 2).sum() / x.z.sum()}))
  17.  
  18. /usr/local/lib/python3.2/site-packages/pandas-0.8.2.dev-py3.2-linux-x86_64.egg/pandas/core/groupby.py in apply(self, func, *args, **kwargs)
  19. 267 applied : type depending on grouped object and function
  20. 268 """
  21. --> 269 return self._python_apply_general(func, *args, **kwargs)
  22. 270
  23. 271 def aggregate(self, func, *args, **kwargs):
  24.  
  25. /usr/local/lib/python3.2/site-packages/pandas-0.8.2.dev-py3.2-linux-x86_64.egg/pandas/core/groupby.py in _python_apply_general(self, func, *args, **kwargs)
  26. 417 group_axes = _get_axes(group)
  27. 418
  28. --> 419 res = func(group, *args, **kwargs)
  29. 420
  30. 421 if not _is_indexed_like(res, group_axes):
  31.  
  32. /home/emarkley/work/src/partner_analysis2/main.py in <lambda>(x)
  33. ----> 1 df.groupby('x').apply(lambda x: pandas.DataFrame({'r': (x.y + x.z).sum() / x.z.sum(), 's': (x.y + x.z ** 2).sum() / x.z.sum()}))
  34.  
  35. /usr/local/lib/python3.2/site-packages/pandas-0.8.2.dev-py3.2-linux-x86_64.egg/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
  36. 371 mgr = self._init_mgr(data, index, columns, dtype=dtype, copy=copy)
  37. 372 elif isinstance(data, dict):
  38. --> 373 mgr = self._init_dict(data, index, columns, dtype=dtype)
  39. 374 elif isinstance(data, ma.MaskedArray):
  40. 375 mask = ma.getmaskarray(data)
  41.  
  42. /usr/local/lib/python3.2/site-packages/pandas-0.8.2.dev-py3.2-linux-x86_64.egg/pandas/core/frame.py in _init_dict(self, data, index, columns, dtype)
  43. 454 # figure out the index, if necessary
  44. 455 if index is None:
  45. --> 456 index = extract_index(data)
  46. 457 else:
  47. 458 index = _ensure_index(index)
  48.  
  49. /usr/local/lib/python3.2/site-packages/pandas-0.8.2.dev-py3.2-linux-x86_64.egg/pandas/core/frame.py in extract_index(data)
  50. 4719
  51. 4720 if not indexes and not raw_lengths:
  52. -> 4721 raise ValueError('If use all scalar values, must pass index')
  53. 4722
  54. 4723 if have_series or have_dicts:
  55.  
  56. ValueError: If use all scalar values, must pass index
  57.  
  58. In [362]: df.groupby('x').apply(lambda x: pandas.DataFrame({'r': (x.y + x.z).sum() / x.z.sum(), 's': (x.y + x.z ** 2).sum() / x.z.sum()}, index=[0]))
  59. Out[362]:
  60. r s
  61. x
  62. a 0 1.316605 -1.672293
  63. b 0 1.608606 -0.972593
  64.  
  65. In [26]: df.groupby('x').apply(lambda x:
  66. Series({'r': (x.y + x.z).sum() / x.z.sum(),
  67. 's': (x.y + x.z ** 2).sum() / x.z.sum()}))
  68. Out[26]:
  69. r s
  70. x
  71. a -0.338590 -0.916635
  72. b 66.655533 102.566146
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement