pandas 滚动平均值-值误差:列必须与键长度相同

lg40wkob  于 2023-01-11  发布在  其他
关注(0)|答案(2)|浏览(145)

我试图计算移动平均线(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,但没有看到这个问题,所以我很困惑。

h79rfbju

h79rfbju1#

你可以通过这种方式实现...

count = '30' #Simple Moving Average with 30 window box
df.dropna(inplace=True) #removing null data
df['MA30'] = df.rolling(count).mean() #Simple Moving Average Formula
fnvucqvd

fnvucqvd2#

我可以解决它:

counts = dfen.groupby(['From date', 'Policy category 1']).size().reset_index(name='counts') 
counts['t_average'] = counts['counts'].rolling(3).mean(center='right') 
print(len(counts['counts'])) print(len(counts['t_average']))

# Plot the counts for each policy code in facets plt.figure(figsize=(20,35)) plt.title("Number of Elements by Year and Policy Code")

for i, code in enumerate(dfen['Policy category 1'].unique()):
    plt.subplot(9, 4, i+1)
    plt.title(code)
    counts_subset = counts[counts['Policy category 1'] == code]
    plt.plot(counts_subset['From date'], counts_subset['t_average'],color=np.random.rand(3,))
    plt.xlim(2000, 2021)
    plt.ylim(0, 40) plt.show()

相关问题