1
減法工作並返回Series
,其索引與groupby
對象(一年中的月份,即1-12)相同。創建新列並將值分配給新列似乎會導致NotImplementedError
。嘗試將值分配給groupby對象的新列時的NotImplementedError
我想從原來的dataframe
減去相應月份的12個月值,即從1月份的每個數據點中減去1
(1月)的值,等等。
test = df
grouped = test.groupby(test.index.month)
values_to_subtract = grouped['A'].median() - test['A'].median()
print values_to_subtract
grouped['new col'] = grouped['B'] - values_to_subtract
print grouped['new col']
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-226-9bff427dc855> in <module>()
3 values_to_subtract = grouped['A'].median() - test['A'].median()
4 print values_to_subtract
----> 5 grouped['new col'] = grouped['B'] - values_to_subtract
6 print grouped['new col']
C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\ops.pyc in wrapper(left, right, name)
503 if hasattr(lvalues, 'values'):
504 lvalues = lvalues.values
--> 505 return left._constructor(wrap_results(na_op(lvalues, rvalues)),
506 index=left.index, name=left.name,
507 dtype=dtype)
C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\ops.pyc in na_op(x, y)
443 try:
444 result = expressions.evaluate(op, str_rep, x, y,
--> 445 raise_on_error=True, **eval_kwargs)
446 except TypeError:
447 if isinstance(y, (pa.Array, pd.Series)):
C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\computation\expressions.pyc in evaluate(op, op_str, a, b, raise_on_error, use_numexpr, **eval_kwargs)
210 if use_numexpr:
211 return _evaluate(op, op_str, a, b, raise_on_error=raise_on_error,
--> 212 **eval_kwargs)
213 return _evaluate_standard(op, op_str, a, b, raise_on_error=raise_on_error)
214
C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\computation\expressions.pyc in _evaluate_standard(op, op_str, a, b, raise_on_error, **eval_kwargs)
63 if _TEST_MODE:
64 _store_test_result(False)
---> 65 return op(a, b)
66
67
C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\ops.pyc in <lambda>(x, y)
70 rmul=arith_method(operator.mul, names('rmul'), op('*'),
71 default_axis=default_axis, reversed=True),
---> 72 rsub=arith_method(lambda x, y: y - x, names('rsub'), op('-'),
73 default_axis=default_axis, reversed=True),
74 rtruediv=arith_method(lambda x, y: operator.truediv(y, x),
C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\groupby.pyc in __getitem__(self, key)
487
488 def __getitem__(self, key):
--> 489 raise NotImplementedError
490
491 def _make_wrapper(self, name):
NotImplementedError:
1 -3.40
2 -3.60
3 -5.30
4 0.15
5 1.80
6 -0.80
7 2.15
8 6.70
9 3.90
10 1.45
11 -0.75
12 -2.70
Name: A, dtype: float64
你是對的,使用'.transform':'values_to_subtract = test ['A']。median() - 分組['A']。transform('median')' 允許我創建一個系列,我可以在分配新列時使用它。 – Bprodz