我想在我的数据集中计算7天滚动平均值。
transaction_week.dtypes
transaction_week.dtypes
zisk_obj float64
datetime_add datetime64[ns]
dtype: object
这是:
transaction_week["mean_weeks_zisk_obj"] = transaction_week.rolling('7D').mean()
这是:
transaction_week["mean_weeks_zisk_obj"] = transaction_week.reset_index().rolling('7D').mean()
给出同样的错误:
Traceback (most recent call last):
File "/usr/lib/python3.8/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/home/vojtam/Desktop/poptavka_cena/venv/lib/python3.8/site-packages/pandas/core/generic.py", line 10868, in rolling
return Rolling(
File "/home/vojtam/Desktop/poptavka_cena/venv/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 158, in __init__
self.validate()
File "/home/vojtam/Desktop/poptavka_cena/venv/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 1532, in validate
raise ValueError("window must be an integer 0 or greater")
ValueError: window must be an integer 0 or greater
我该怎么修呢?
这个问题对我没有帮助:Rolling Mean with Time Offset Pandas
发布于 2021-12-15 11:33:22
我是带着这个解决方案来的,但肯定有更好的办法:
transaction_week['week'] = transaction_week['datetime_add'].apply(lambda x: x.week)
transaction_week['mean_weeks_zisk_obj'] = transaction_week.groupby(['week'])["zisk_obj"].transform('mean')
https://stackoverflow.com/questions/70362414
复制相似问题