我有一个pandas数据帧df
:
>>> df
net_inventory sales yr temp_stk_id
STK_ID RPT_Date
34_STK43 20080331 282.1603 359.4644 2008 34_STK43
20080630 297.4760 716.7633 2008 34_STK43
20080930 283.6312 1105.6332 2008 34_STK43
20081231 296.9090 1380.4886 2008 34_STK43
20090331 276.2348 363.3449 2009 34_STK43
20090630 288.0186 753.6347 2009 34_STK43
20090930 287.0811 1173.3760 2009 34_STK43
>>> df.dtypes
net_inventory float64
sales float64
yr object
temp_stk_id object
然后我想为“net_inventory”和“sales”列做一个“expanding_mean()”,groupby ['temp_stk_id','yr']
:
>>> df.groupby(['temp_stk_id','yr']).apply(lambda x: pd.expanding_mean(x))
它给出了以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python\Lib\site-packages\pandas\core\groupby.py", line 322, in apply
return self._python_apply_general(f)
File "D:\Python\Lib\site-packages\pandas\core\groupby.py", line 325, in _python_apply_general
keys, values, mutated = self.grouper.apply(f, self.obj, self.axis)
File "D:\Python\Lib\site-packages\pandas\core\groupby.py", line 596, in apply
res = f(group)
File "D:\Python\Lib\site-packages\pandas\core\groupby.py", line 321, in <lambda>
f = lambda g: func(g, *args, **kwargs)
File "<stdin>", line 1, in <lambda>
File "D:\Python\Lib\site-packages\pandas\stats\moments.py", line 697, in f
time_rule=time_rule, **kwargs)
File "D:\Python\Lib\site-packages\pandas\stats\moments.py", line 281, in _rolling_moment
return_hook, values = _process_data_structure(arg)
File "D:\Python\Lib\site-packages\pandas\stats\moments.py", line 326, in _process_data_structure
values = values.astype(float)
ValueError: invalid literal for float(): 34_STK43
我认为这是因为expanding_mean()不适用于对象类型temp_stk_id
和'yr'列,所以我想:
df.groupby(['temp_stk_id','yr']).apply(lambda x: pd.expanding_mean(x) for all columns except [`temp_stk_id`, 'yr'])
或
df.groupby(['temp_stk_id','yr']).apply(lambda x: pd.expanding_mean(x) for specified columns)
怎么做?
答案 0 :(得分:3)
这看起来像是transform
的工作:
In [10]: g = df1.groupby(['temp_stk_id','yr'])
In [11]: g.transform(pd.expanding_mean)
Out[11]:
net_inventory sales
STK_ID RPT_Date
34_STK43 20080331 282.160300 359.464400
20080630 289.818150 538.113850
20080930 287.755833 727.286967
20081231 290.044125 890.587375
20090331 276.234800 363.344900
20090630 282.126700 558.489800
20090930 283.778167 763.451867