我仍然不清楚您想在第 5 周做什么,但是这将为您提供当月一周的数字。
# this will only work if the first date belongs to the first week of that month
# and if there is only one date per week
wom = ( # week of month
df.index.to_series()
.groupby([df.index.year, df.index.month])
.cumcount() + 1 # create the 1, 2, 3, 4, 5 tags for week of month
)
# you can keep it as a separate indexer
sales_month_over = df.loc[wom < 5, :]
# or you can create a MultiIndex
df.index = pd.MultiIndex.from_arrays([df.index, wom], names=['date', 'wom'])
sales_month_over = df.loc[df.index.get_level_values('wom') < 5]
fifth_weeks = df.loc[~df.index.isin(sales_month_over.index)]
>>> print(sales_month_over)
0 1 2 3 4 5 6 7 ... 107 108 109 110 111 112 113 114
date wom ...
2019-01-05 1 78 135 66 68 64 69 109 70 ... 58 166 122 81 162 193 74 196
2019-01-12 2 138 191 130 80 177 60 139 114 ... 147 188 59 149 126 131 133 178
2019-01-19 3 198 111 181 145 91 60 128 184 ... 80 54 110 152 114 165 86 68
2019-01-26 4 154 169 134 90 173 122 140 182 ... 186 140 150 65 68 92 128 169
2019-02-02 1 105 55 82 74 125 163 91 95 ... 199 67 116 155 128 162 133 110
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2020-05-23 4 163 176 92 78 88 64 55 79 ... 142 156 134 158 63 157 77 75
2020-06-06 1 133 167 117 91 180 106 169 154 ... 58 170 115 101 108 89 57 56
2020-06-13 2 78 86 93 192 53 143 182 184 ... 193 139 68 179 55 61 131 167
2020-06-20 3 119 123 91 145 71 193 97 182 ... 146 163 52 120 195 56 153 126
2020-06-27 4 50 191 72 89 76 151 166 89 ... 132 95 111 134 83 64 188 150
[72 rows x 115 columns]
>>> print(fifth_weeks)
0 1 2 3 4 5 6 7 ... 107 108 109 110 111 112 113 114
date wom ...
2019-03-30 5 199 120 147 81 61 85 132 174 ... 99 162 177 104 118 168 117 92
2019-06-29 5 113 72 92 64 192 188 51 164 ... 143 137 126 117 162 157 53 102
2019-08-31 5 129 192 60 156 153 137 183 117 ... 155 115 57 92 124 99 143 119
2019-11-30 5 133 190 156 179 79 107 158 118 ... 165 180 91 139 176 159 61 103
2020-02-29 5 123 195 182 170 155 145 189 84 ... 152 115 74 128 190 72 53 104
2020-05-30 5 176 121 132 155 60 57 120 182 ... 57 136 52 190 152 168 65 164
[6 rows x 115 columns]
现在您可以获取与上个月相比的百分比变化
sales_month_over = sales_month_over.groupby(level='wom').pct_change()
>>> print(sales_month_over)
0 1 2 3 ... 111 112 113 114
date wom ...
2019-01-05 1 NaN NaN NaN NaN ... NaN NaN NaN NaN
2019-01-12 2 NaN NaN NaN NaN ... NaN NaN NaN NaN
2019-01-19 3 NaN NaN NaN NaN ... NaN NaN NaN NaN
2019-01-26 4 NaN NaN NaN NaN ... NaN NaN NaN NaN
2019-02-02 1 -0.063953 -0.259259 -0.259067 -0.401961 ... 0.084416 0.452055 -0.481250 0.012579
... ... ... ... ... ... ... ... ... ...
2020-05-23 4 -0.554878 -0.191860 0.285714 0.265734 ... -0.658824 0.943820 0.444444 0.950000
2020-06-06 1 -0.598540 1.763889 -0.155844 -0.338983 ... -0.248000 -0.006757 -0.512821 -0.043243
2020-06-13 2 0.130435 0.390244 0.423358 -0.460177 ... -0.013158 -0.167702 0.015385 0.305785
2020-06-20 3 -0.437500 -0.100000 1.650000 0.175439 ... 0.666667 -0.088235 0.155556 0.246753
2020-06-27 4 0.452055 -0.474820 -0.374269 -0.414365 ... 1.172414 -0.710983 -0.525641 -0.521368
[72 rows x 115 columns]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句