我试图计算移动平均线(3年),但我得到了一个不同大小的对象。
counts = dfen.groupby(['From date', 'Policy category 1']).size().reset_index(name='counts')
t = counts['counts']
counts['t_average'] = t.rolling(2).mean()
我得到:
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_6032\1024416527.py in <module>
2 counts = dfen.groupby(['From date', 'Policy category 1']).size().reset_index(name='counts')
3 t = counts['counts']
4 counts[t_average] = t.rolling(2).sum()
5
6
~\anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
3641 self._setitem_frame(key, value)
3642 elif isinstance(key, (Series, np.ndarray, list, Index)):
3643 self._setitem_array(key, value)
3644 elif isinstance(value, DataFrame):
3645 self._set_item_frame_value(key, value)
~\anaconda3\lib\site-packages\pandas\core\frame.py in _setitem_array(self, key, value)
3700
3701 else:
3702 self._iset_not_inplace(key, value)
3703
3704 def _iset_not_inplace(self, key, value):
~\anaconda3\lib\site-packages\pandas\core\frame.py in _iset_not_inplace(self, key, value)
3719 if self.columns.is_unique:
3720 if np.shape(value)[-1] != len(key):
3721 raise ValueError("Columns must be same length as key")
3722
3723 for i, col in enumerate(key):
ValueError: Columns must be same length as key
有人知道我做错了什么吗?先谢了
我读了Pandas的文档,它说NaN是自动填充的开始和结束值(Iàm计算一个居中的平均值)...和文档中的例子是一样的...(https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html)我也搜索了Stackoverflow,但没有看到这个问题,所以我很困惑。
2条答案
按热度按时间h79rfbju1#
你可以通过这种方式实现...
fnvucqvd2#
我可以解决它: