# 6、每个用户按周求和并差分(一周7天,年度分开),并求取差分结果的基本统计量,统计量同三。 res4 = pd.DataFrame() for col in data_t.columns: # 按周求和 data_week = data_t[col].resample('W').sum() # 按周差分 data_week_diff = data_week.diff(periods=1) # 去掉第一个空值 data_week_diff = data_week_diff.dropna() # 求取差分结果的基本统计量 res = pd.Series() res['最大值'] = data_week_diff.max() res['最小值'] = data_week_diff.min() res['均值'] = data_week_diff.mean() res['中位数'] = data_week_diff.median() res['和'] = data_week_diff.sum() res['方差'] = data_week_diff.var() res['偏度'] = data_week_diff.skew() res['峰度'] = data_week_diff.kurt() # 将结果存入res4中 res4[col] = res print("每个用户按周求和并差分的基本统计量") print(res4)修改运行代码
时间: 2024-02-07 15:03:41 浏览: 117
以下是修改后的运行代码:
```python
res4 = pd.DataFrame()
for col in data_t.columns:
# 按周求和
data_week = data_t[col].resample('W').sum()
# 按周差分
data_week_diff = data_week.diff(periods=1)
# 去掉第一个空值
data_week_diff = data_week_diff.dropna()
# 按年度分组
data_year_groups = data_week_diff.groupby(data_week_diff.index.year)
# 求取差分结果的基本统计量
res = pd.DataFrame()
for year, group in data_year_groups:
year_res = pd.Series()
year_res['最大值'] = group.max()
year_res['最小值'] = group.min()
year_res['均值'] = group.mean()
year_res['中位数'] = group.median()
year_res['和'] = group.sum()
year_res['方差'] = group.var()
year_res['偏度'] = group.skew()
year_res['峰度'] = group.kurt()
res[year] = year_res
# 将结果存入res4中
res4[col] = res.stack()
print("每个用户按周求和并差分的基本统计量")
print(res4)
```
修改后的代码对每个用户的数据按年度分组,并对每个年度计算基本统计量。同时,程序还将结果存储在一个DataFrame中,方便查看和分析。
阅读全文