Python groups and plots data by year, month, and day

Reference: pd.Grouper
is as follows: The X axis is the date from 2014-7-24 to 2015-3-5, and the Y axis is the daily sales.
Goal: Turn sales aggregated by day into aggregated by week, month, and year.
insert image description here
insert image description here
Solution:

df_new=df_normal.groupby([pd.Grouper(key='日期',freq='W')])[['销量']].sum().reset_index()
df_new

The result is as follows: it can be seen that the weekly sales are added up


日期	销量
0	2014-08-03	5698.10
1	2014-08-10	20778.00
2	2014-08-17	19775.20
3	2014-08-24	18904.80
4	2014-08-31	20946.10
5	2014-09-07	21164.20
6	2014-09-14	18832.70
7	2014-09-21	18376.70
8	2014-09-28	24253.44
9	2014-10-05	20048.80
10	2014-10-12	19328.00
11	2014-10-19	17779.10
12	2014-10-26	15300.10
13	2014-11-02	15130.20
14	2014-11-09	15395.80
15	2014-11-16	19114.90
16	2014-11-23	17706.70
17	2014-11-30	17485.20
18	2014-12-07	15847.30
19	2014-12-14	19300.60
20	2014-12-21	13925.50
21	2014-12-28	15134.40
22	2015-01-04	20265.00
23	2015-01-11	10048.80
24	2015-01-18	18539.80
25	2015-01-25	18447.70
26	2015-02-01	17835.50
27	2015-02-08	19593.20
28	2015-02-15	14531.70
29	2015-02-22	23654.10
30	2015-03-01	17901.30

insert image description here
The results after aggregating by days:
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/wxfighting/article/details/123987888