importpandasaspdimportnumpyasnp# generating data consisting of weekly sales for the timeperiod Jan,2022 to Jan,2023dates=pd.date_range('2022-01-01', '2023-01-05', freq='1 W')
sales_val=np.linspace(1000, 2000,len(dates) )
data= {'date':dates,
'sales': sales_val}
# Load the datadf=pd.DataFrame(data)
# Convert the 'date' column to a datetime typedf['date'] =pd.to_datetime(df['date'])
df.sample(5)
importmatplotlib.pyplotaspltimportseabornassns# Set the 'date' column as the index,# and Group the data by month using resamplegrouped=df.set_index('date').resample('M').mean()
print("Grouping is done on monthly basis using resample method:\n", grouped)
# plot the average of monthly salessns.lineplot(grouped.index, grouped['sales'])
plt.xlabel("Date")
plt.ylabel("Average Monthly Sales")
plt.grid(True)
plt.title("Average Monthly sales with respect to month")
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
在本例中,我们首先将' date '列转换为日期类型,然后将其设置为DataFrame的索引。然后使用重采样方法按月分组数据,并计算每个月的“sales”列的平均值。结果是一个新的DF,每个月有一行,还包含该月“sales”列的平均值。
importmatplotlib.pyplotaspltimportseabornassns# Group the data by month using pd.Grouper and calculate monthly averagegrouped=df.groupby(pd.Grouper(key='date', freq='M')).mean()
print("Grouping is done on monthly basis using pandas.Grouper and groupby method:\n", grouped)
# plot the average of monthly salessns.lineplot(grouped.index, grouped['sales'])
plt.xlabel("Date")
plt.ylabel("Average Monthly Sales")
plt.grid(True)
plt.title("Average Monthly sales with respect to month using pd.Grouper and groupby ")3.Usingdtaccessorwithgroupby:
importmatplotlib.pyplotaspltimportseabornassns# Group the data by month using dt and calculate monthly averagegrouped=df.groupby(df['date'].dt.to_period("M")).mean()
print("Grouping is done on monthly basis using dt and groupby method:\n", grouped)