mdbq 2.6.1__py3-none-any.whl → 2.6.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -93,6 +93,18 @@ class MysqlDatasQuery:
93
93
  username, password, host, port = get_myconf.select_config_values(target_service=target_service, database='mysql')
94
94
  self.download = s_query.QueryDatas(username=username, password=password, host=host, port=port)
95
95
 
96
+ @staticmethod
97
+ def try_except(func): # 在类内部定义一个异常处理方法
98
+ @wraps(func)
99
+ def wrapper(*args, **kwargs):
100
+ try:
101
+ return func(*args, **kwargs)
102
+ except Exception as e:
103
+ print(f'{func.__name__}, {e}') # 将异常信息返回
104
+
105
+ return wrapper
106
+
107
+ @try_except
96
108
  def tg_wxt(self):
97
109
  start_date, end_date = self.months_data(num=self.months)
98
110
  projection = {
@@ -108,6 +120,7 @@ class MysqlDatasQuery:
108
120
  '自然流量曝光量': 1,
109
121
  '直接成交笔数': 1,
110
122
  '直接成交金额': 1,
123
+ '店铺名称': 1,
111
124
  }
112
125
  df = self.download.data_to_df(
113
126
  db_name='推广数据2',
@@ -118,6 +131,7 @@ class MysqlDatasQuery:
118
131
  )
119
132
  return df
120
133
 
134
+ @try_except
121
135
  def syj(self):
122
136
  start_date, end_date = self.months_data(num=self.months)
123
137
  projection = {
@@ -142,6 +156,7 @@ class MysqlDatasQuery:
142
156
  )
143
157
  return df
144
158
 
159
+ @try_except
145
160
  def tg_rqbb(self):
146
161
  start_date, end_date = self.months_data(num=self.months)
147
162
  projection = {
@@ -157,6 +172,7 @@ class MysqlDatasQuery:
157
172
  '直接成交笔数': 1,
158
173
  '直接成交金额': 1,
159
174
  '人群名字': 1,
175
+ '店铺名称': 1,
160
176
  }
161
177
  df = self.download.data_to_df(
162
178
  db_name='推广数据2',
@@ -167,6 +183,7 @@ class MysqlDatasQuery:
167
183
  )
168
184
  return df
169
185
 
186
+ @try_except
170
187
  def tg_gjc(self):
171
188
  start_date, end_date = self.months_data(num=self.months)
172
189
  projection = {
@@ -183,6 +200,7 @@ class MysqlDatasQuery:
183
200
  '总成交金额': 1,
184
201
  '直接成交笔数': 1,
185
202
  '直接成交金额': 1,
203
+ '店铺名称': 1,
186
204
  }
187
205
  df = self.download.data_to_df(
188
206
  db_name='推广数据2',
@@ -193,6 +211,7 @@ class MysqlDatasQuery:
193
211
  )
194
212
  return df
195
213
 
214
+ @try_except
196
215
  def tg_cjzb(self):
197
216
  start_date, end_date = self.months_data(num=self.months)
198
217
  projection = {
@@ -210,6 +229,7 @@ class MysqlDatasQuery:
210
229
  '总成交金额': 1,
211
230
  '直接成交笔数': 1,
212
231
  '直接成交金额': 1,
232
+ '店铺名称': 1,
213
233
  }
214
234
  df = self.download.data_to_df(
215
235
  db_name='推广数据2',
@@ -220,6 +240,7 @@ class MysqlDatasQuery:
220
240
  )
221
241
  return df
222
242
 
243
+ @try_except
223
244
  def pxb_zh(self):
224
245
  start_date, end_date = self.months_data(num=self.months)
225
246
  projection = {
@@ -235,6 +256,7 @@ class MysqlDatasQuery:
235
256
  '成交笔数': 1,
236
257
  '成交金额': 1,
237
258
  # '成交访客数': 1
259
+ '店铺名称': 1,
238
260
  }
239
261
  df = self.download.data_to_df(
240
262
  db_name='推广数据2',
@@ -245,6 +267,7 @@ class MysqlDatasQuery:
245
267
  )
246
268
  return df
247
269
 
270
+ @try_except
248
271
  def idbm(self):
249
272
  """ 用生意经日数据制作商品 id 和编码对照表 """
250
273
  data_values = self.download.columns_to_list(
@@ -255,16 +278,18 @@ class MysqlDatasQuery:
255
278
  df = pd.DataFrame(data=data_values)
256
279
  return df
257
280
 
281
+ @try_except
258
282
  def sp_picture(self):
259
283
  """ 用生意经日数据制作商品 id 和编码对照表 """
260
284
  data_values = self.download.columns_to_list(
261
- db_name='属性设置2',
262
- table_name='商品素材导出',
285
+ db_name='属性设置3',
286
+ table_name='商品素材中心',
263
287
  columns_name=['日期', '商品id', '商品白底图', '方版场景图'],
264
288
  )
265
289
  df = pd.DataFrame(data=data_values)
266
290
  return df
267
291
 
292
+ @try_except
268
293
  def dplyd(self):
269
294
  """ 新旧版取的字段是一样的 """
270
295
  start_date, end_date = self.months_data(num=self.months)
@@ -278,16 +303,19 @@ class MysqlDatasQuery:
278
303
  '支付买家数': 1,
279
304
  '支付转化率': 1,
280
305
  '加购人数': 1,
306
+ '店铺名称': 1,
281
307
  }
282
308
  df = self.download.data_to_df(
283
- db_name='生意参谋2',
284
- table_name='店铺来源_日数据',
309
+ db_name='生意参谋3',
310
+ table_name='店铺流量来源构成',
285
311
  start_date=start_date,
286
312
  end_date=end_date,
287
313
  projection=projection,
288
314
  )
315
+ # df = df[df['店铺名称'] == '万里马官方旗舰店']
289
316
  return df
290
317
 
318
+ @try_except
291
319
  def dplyd_old(self):
292
320
  start_date, end_date = self.months_data(num=self.months)
293
321
  projection = {
@@ -310,16 +338,18 @@ class MysqlDatasQuery:
310
338
  )
311
339
  return df
312
340
 
341
+ @try_except
313
342
  def sp_cost(self):
314
343
  """ 电商定价 """
315
344
  data_values = self.download.columns_to_list(
316
- db_name='属性设置2',
345
+ db_name='属性设置3',
317
346
  table_name='电商定价',
318
347
  columns_name=['日期', '款号', '年份季节', '吊牌价', '商家平台', '成本价', '天猫页面价', '天猫中促价'],
319
348
  )
320
349
  df = pd.DataFrame(data=data_values)
321
350
  return df
322
351
 
352
+ @try_except
323
353
  def jdjzt(self):
324
354
  start_date, end_date = self.months_data(num=self.months)
325
355
  projection = {
@@ -337,15 +367,18 @@ class MysqlDatasQuery:
337
367
  '直接加购数': 1,
338
368
  '总加购数': 1,
339
369
  'spu id': 1,
370
+ '店铺名称':1,
340
371
  }
341
372
  df = self.download.data_to_df(
342
- db_name='京东数据2',
373
+ db_name='京东数据3',
343
374
  table_name='推广数据_京准通',
344
375
  start_date=start_date,
345
376
  end_date=end_date,
346
377
  projection=projection,
347
378
  )
348
379
  return df
380
+
381
+ @try_except
349
382
  def jdqzyx(self):
350
383
  start_date, end_date = self.months_data(num=self.months)
351
384
  projection = {
@@ -361,13 +394,15 @@ class MysqlDatasQuery:
361
394
  '核心位置点击量': 1,
362
395
  }
363
396
  df = self.download.data_to_df(
364
- db_name='京东数据2',
365
- table_name='推广数据_全站营销',
397
+ db_name='京东数据3',
398
+ table_name='推广数据_全站营销', # 暂缺
366
399
  start_date=start_date,
367
400
  end_date=end_date,
368
401
  projection=projection,
369
402
  )
370
403
  return df
404
+
405
+ @try_except
371
406
  def jd_gjc(self):
372
407
  start_date, end_date = self.months_data(num=self.months)
373
408
  projection = {
@@ -388,19 +423,21 @@ class MysqlDatasQuery:
388
423
  '总订单行': 1,
389
424
  '总订单金额': 1,
390
425
  '总加购数': 1,
391
- '下单新客数_去重': 1,
426
+ '下单新客数(去重)': 1,
392
427
  '领券数': 1,
393
428
  '商品关注数': 1,
394
- '店铺关注数': 1
429
+ '店铺关注数': 1,
395
430
  }
396
431
  df = self.download.data_to_df(
397
- db_name='京东数据2',
432
+ db_name='京东数据3',
398
433
  table_name='推广数据_关键词报表',
399
434
  start_date=start_date,
400
435
  end_date=end_date,
401
436
  projection=projection,
402
437
  )
403
438
  return df
439
+
440
+ @try_except
404
441
  def sku_sales(self):
405
442
  start_date, end_date = self.months_data(num=self.months)
406
443
  projection = {
@@ -415,13 +452,15 @@ class MysqlDatasQuery:
415
452
  '加购人数': 1,
416
453
  }
417
454
  df = self.download.data_to_df(
418
- db_name='京东数据2',
419
- table_name='sku_商品明细',
455
+ db_name='京东数据3',
456
+ table_name='京东商智_sku_商品明细',
420
457
  start_date=start_date,
421
458
  end_date=end_date,
422
459
  projection=projection,
423
460
  )
424
461
  return df
462
+
463
+ @try_except
425
464
  def spu_sales(self):
426
465
  start_date, end_date = self.months_data(num=self.months)
427
466
  projection = {
@@ -436,8 +475,8 @@ class MysqlDatasQuery:
436
475
  '加购人数': 1,
437
476
  }
438
477
  df = self.download.data_to_df(
439
- db_name='京东数据2',
440
- table_name='spu_商品明细',
478
+ db_name='京东数据3',
479
+ table_name='京东商智_spu_商品明细',
441
480
  start_date=start_date,
442
481
  end_date=end_date,
443
482
  projection=projection,
@@ -453,6 +492,7 @@ class MysqlDatasQuery:
453
492
  start_date = f'{start_date.year}-{start_date.month}-01' # 替换为 n 月以前的第一天
454
493
  return pd.to_datetime(start_date), pd.to_datetime(end_date)
455
494
 
495
+ @try_except
456
496
  def tm_search(self):
457
497
  start_date, end_date = self.months_data(num=self.months)
458
498
  projection = {
@@ -466,9 +506,10 @@ class MysqlDatasQuery:
466
506
  '下单买家数': 1,
467
507
  '加购人数': 1,
468
508
  '新访客': 1,
509
+ '店铺名称': 1,
469
510
  }
470
511
  df = self.download.data_to_df(
471
- db_name='生意参谋2',
512
+ db_name='生意参谋3',
472
513
  table_name='店铺来源_手淘搜索',
473
514
  start_date=start_date,
474
515
  end_date=end_date,
@@ -476,6 +517,7 @@ class MysqlDatasQuery:
476
517
  )
477
518
  return df
478
519
 
520
+ @try_except
479
521
  def zb_ccfx(self):
480
522
  start_date, end_date = self.months_data(num=self.months)
481
523
  projection = {
@@ -510,9 +552,10 @@ class MysqlDatasQuery:
510
552
  '退款金额(元)': 1,
511
553
  '预售定金支付金额(元)': 1,
512
554
  '预售预估总金额(元)': 1,
555
+ '店铺名称': 1,
513
556
  }
514
557
  df = self.download.data_to_df(
515
- db_name='生意参谋2',
558
+ db_name='生意参谋3',
516
559
  table_name='直播场次分析',
517
560
  start_date=start_date,
518
561
  end_date=end_date,
@@ -520,6 +563,7 @@ class MysqlDatasQuery:
520
563
  )
521
564
  return df
522
565
 
566
+ # @try_except
523
567
  def tg_by_day(self):
524
568
  """
525
569
  汇总各个店铺的推广数据,按日汇总
@@ -534,6 +578,7 @@ class MysqlDatasQuery:
534
578
  '总购物车数': 1,
535
579
  '总成交笔数': 1,
536
580
  '总成交金额': 1,
581
+ '店铺名称': 1,
537
582
  }
538
583
  df_tm = self.download.data_to_df(
539
584
  db_name='推广数据2',
@@ -544,7 +589,7 @@ class MysqlDatasQuery:
544
589
  )
545
590
  df_tm.rename(columns={'场景名字': '营销场景'}, inplace=True)
546
591
  df_tm = df_tm.groupby(
547
- ['日期', '营销场景', '花费'],
592
+ ['日期', '店铺名称', '营销场景', '花费'],
548
593
  as_index=False).agg(
549
594
  **{
550
595
  '展现量': ('展现量', np.max),
@@ -554,7 +599,6 @@ class MysqlDatasQuery:
554
599
  '成交金额': ('总成交金额', np.max)
555
600
  }
556
601
  )
557
- df_tm.insert(loc=1, column='店铺', value='WLM天猫旗舰店') # df中添加列
558
602
 
559
603
  df_tb = self.download.data_to_df(
560
604
  db_name='推广数据_淘宝店',
@@ -568,7 +612,7 @@ class MysqlDatasQuery:
568
612
  # df_tb.to_csv('/Users/xigua/Downloads/test.csv', index=False, header=True, encoding='utf-8_sig')
569
613
  df_tb.rename(columns={'场景名字': '营销场景'}, inplace=True)
570
614
  df_tb = df_tb.groupby(
571
- ['日期', '营销场景', '花费'],
615
+ ['日期', '店铺名称', '营销场景', '花费'],
572
616
  as_index=False).agg(
573
617
  **{
574
618
  '展现量': ('展现量', np.max),
@@ -578,7 +622,6 @@ class MysqlDatasQuery:
578
622
  '成交金额': ('总成交金额', np.max)
579
623
  }
580
624
  )
581
- df_tb.insert(loc=1, column='店铺', value='淘宝c店') # df中添加列
582
625
 
583
626
  projection = {
584
627
  '日期': 1,
@@ -589,6 +632,7 @@ class MysqlDatasQuery:
589
632
  '宝贝加购数': 1,
590
633
  '成交笔数': 1,
591
634
  '成交金额': 1,
635
+ '店铺名称': 1,
592
636
  }
593
637
  df_tm_pxb = self.download.data_to_df(
594
638
  db_name='推广数据2',
@@ -599,7 +643,7 @@ class MysqlDatasQuery:
599
643
  )
600
644
  df_tm_pxb = df_tm_pxb[df_tm_pxb['报表类型'] == '账户']
601
645
  df_tm_pxb = df_tm_pxb.groupby(
602
- ['日期', '报表类型', '消耗'],
646
+ ['日期', '店铺名称', '报表类型', '消耗'],
603
647
  as_index=False).agg(
604
648
  **{
605
649
  '展现量': ('展现量', np.max),
@@ -611,7 +655,6 @@ class MysqlDatasQuery:
611
655
  )
612
656
  df_tm_pxb.rename(columns={'报表类型': '营销场景', '消耗': '花费'}, inplace=True)
613
657
  df_tm_pxb['营销场景'] = '品销宝'
614
- df_tm_pxb.insert(loc=1, column='店铺', value='WLM天猫旗舰店') # df中添加列
615
658
 
616
659
  projection = {
617
660
  '日期': 1,
@@ -628,15 +671,16 @@ class MysqlDatasQuery:
628
671
  '直接加购数': 1,
629
672
  '总加购数': 1,
630
673
  'spu id': 1,
674
+ '店铺名称': 1,
631
675
  }
632
676
  df_jd = self.download.data_to_df(
633
- db_name='京东数据2',
677
+ db_name='京东数据3',
634
678
  table_name='推广数据_京准通',
635
679
  start_date=start_date,
636
680
  end_date=end_date,
637
681
  projection=projection,
638
682
  )
639
- df_jd = df_jd.groupby(['日期', '产品线', '触发sku id', '跟单sku id', 'spu id', '花费', '展现数', '点击数'],
683
+ df_jd = df_jd.groupby(['日期', '店铺名称', '产品线', '触发sku id', '跟单sku id', 'spu id', '花费', '展现数', '点击数'],
640
684
  as_index=False).agg(
641
685
  **{'直接订单行': ('直接订单行', np.max),
642
686
  '直接订单金额': ('直接订单金额', np.max),
@@ -646,10 +690,9 @@ class MysqlDatasQuery:
646
690
  '加购量': ('总加购数', np.max),
647
691
  }
648
692
  )
649
- df_jd = df_jd[['日期', '产品线', '花费', '展现数', '点击数', '加购量', '成交笔数', '成交金额']]
693
+ df_jd = df_jd[['日期', '店铺名称', '产品线', '花费', '展现数', '点击数', '加购量', '成交笔数', '成交金额']]
650
694
  df_jd.rename(columns={'产品线': '营销场景', '展现数': '展现量', '点击数': '点击量'}, inplace=True)
651
695
  df_jd = df_jd[df_jd['花费'] > 0]
652
- df_jd.insert(loc=1, column='店铺', value='京东箱包') # df中添加列
653
696
 
654
697
  projection = {
655
698
  '日期': 1,
@@ -662,15 +705,16 @@ class MysqlDatasQuery:
662
705
  '全站费比': 1,
663
706
  '核心位置展现量': 1,
664
707
  '核心位置点击量': 1,
708
+ '店铺名称': 1,
665
709
  }
666
710
  df_jd_qzyx = self.download.data_to_df(
667
- db_name='京东数据2',
711
+ db_name='京东数据3',
668
712
  table_name='推广数据_全站营销',
669
713
  start_date=start_date,
670
714
  end_date=end_date,
671
715
  projection=projection,
672
716
  )
673
- df_jd_qzyx = df_jd_qzyx.groupby(['日期', '产品线', '花费'], as_index=False).agg(
717
+ df_jd_qzyx = df_jd_qzyx.groupby(['日期', '店铺名称', '产品线', '花费'], as_index=False).agg(
674
718
  **{'全站roi': ('全站roi', np.max),
675
719
  '成交金额': ('全站交易额', np.max),
676
720
  '成交笔数': ('全站订单行', np.max),
@@ -681,14 +725,14 @@ class MysqlDatasQuery:
681
725
  }
682
726
  )
683
727
  df_jd_qzyx.rename(columns={'产品线': '营销场景'}, inplace=True)
684
- df_jd_qzyx = df_jd_qzyx[['日期', '营销场景', '花费', '展现量', '点击量', '成交笔数', '成交金额']]
728
+ df_jd_qzyx = df_jd_qzyx[['日期', '店铺名称', '营销场景', '花费', '展现量', '点击量', '成交笔数', '成交金额']]
685
729
  df_jd_qzyx = df_jd_qzyx[df_jd_qzyx['花费'] > 0]
686
- df_jd_qzyx.insert(loc=1, column='店铺', value='京东箱包') # df中添加列
687
730
 
688
731
  _datas = [item for item in [df_tm, df_tb, df_tm_pxb, df_jd, df_jd_qzyx] if len(item) > 0] # 阻止空的 dataframe
689
732
  df = pd.concat(_datas, axis=0, ignore_index=True)
690
733
  return df
691
734
 
735
+ @try_except
692
736
  def aikucun_bd_spu(self):
693
737
  start_date, end_date = self.months_data(num=self.months)
694
738
  projection = {
@@ -725,6 +769,7 @@ class MysqlDatasQuery:
725
769
  'sku数_成交': 1,
726
770
  '营销后供货额': 1,
727
771
  '营销后供货价': 1,
772
+ '店铺名称': 1,
728
773
  }
729
774
  projection = {}
730
775
  df = self.download.data_to_df(
@@ -736,6 +781,7 @@ class MysqlDatasQuery:
736
781
  )
737
782
  return df
738
783
 
784
+ @try_except
739
785
  def dmp_crowd(self):
740
786
  start_date, end_date = self.months_data(num=self.months)
741
787
  projection = {
@@ -806,7 +852,7 @@ class GroupBy:
806
852
 
807
853
  return wrapper
808
854
 
809
- @try_except
855
+ # @try_except
810
856
  def groupby(self, df, table_name, is_maximize=True):
811
857
  """
812
858
  self.is_maximize: 是否最大转化数据
@@ -849,7 +895,7 @@ class GroupBy:
849
895
  '直接成交金额': float,
850
896
  }, errors='raise')
851
897
  if is_maximize:
852
- df = df.groupby(['日期', '营销场景', '商品id', '花费', '展现量', '点击量'], as_index=False).agg(
898
+ df = df.groupby(['日期', '店铺名称', '营销场景', '商品id', '花费', '展现量', '点击量'], as_index=False).agg(
853
899
  **{'加购量': ('加购量', np.max),
854
900
  '成交笔数': ('成交笔数', np.max),
855
901
  '成交金额': ('成交金额', np.max),
@@ -859,7 +905,7 @@ class GroupBy:
859
905
  }
860
906
  )
861
907
  else:
862
- df = df.groupby(['日期', '营销场景', '商品id', '花费', '展现量', '点击量'], as_index=False).agg(
908
+ df = df.groupby(['日期', '店铺名称', '营销场景', '商品id', '花费', '展现量', '点击量'], as_index=False).agg(
863
909
  **{
864
910
  '加购量': ('加购量', np.min),
865
911
  '成交笔数': ('成交笔数', np.min),
@@ -870,7 +916,7 @@ class GroupBy:
870
916
  }
871
917
  )
872
918
  df.insert(loc=1, column='推广渠道', value='万相台无界版') # df中插入新列
873
- df_new = df.groupby(['日期', '商品id'], as_index=False).agg(
919
+ df_new = df.groupby(['日期', '店铺名称', '商品id'], as_index=False).agg(
874
920
  **{
875
921
  '花费': ('花费', np.sum),
876
922
  '成交笔数': ('成交笔数', np.max),
@@ -898,7 +944,8 @@ class GroupBy:
898
944
  else:
899
945
  year_my = today.year
900
946
  # 截取 从上月1日 至 今天的花费数据, 推广款式按此数据从高到低排序(商品图+排序)
901
- df_pic = df.groupby(['日期', '商品id'], as_index=False).agg({'花费': 'sum'})
947
+ df_pic_lin = df[df['店铺名称'] == '万里马官方旗舰店']
948
+ df_pic = df_pic_lin.groupby(['日期', '商品id'], as_index=False).agg({'花费': 'sum'})
902
949
  df_pic = df_pic[~df_pic['商品id'].isin([''])] # 指定列中删除包含空值的行
903
950
  df_pic = df_pic[(df_pic['日期'] >= f'{year_my}-{last_month.month}-01')]
904
951
  df_pic = df_pic.groupby(['商品id'], as_index=False).agg({'花费': 'sum'})
@@ -906,9 +953,9 @@ class GroupBy:
906
953
  df_pic.reset_index(inplace=True)
907
954
  df_pic['index'] = df_pic['index'] + 100
908
955
  df_pic.rename(columns={'index': '商品索引'}, inplace=True)
909
- df_pic_new = pd.merge(df, df_pic, how='left', on=['商品id'])
956
+ df_pic_new = pd.merge(df_pic_lin, df_pic, how='left', on=['商品id'])
910
957
  df_pic_new['商品索引'].fillna(1000, inplace=True)
911
- self.sp_index_datas = df_pic_new[['商品id', '商品索引']]
958
+ self.sp_index_datas = df_pic_new[['商品id', '商品索引']] # 商品索引表_主推排序调用
912
959
  return df
913
960
  elif '商品索引表' in table_name:
914
961
  return df
@@ -916,6 +963,7 @@ class GroupBy:
916
963
  df.drop_duplicates(
917
964
  subset=[
918
965
  '日期',
966
+ '店铺名称',
919
967
  'spu_id',
920
968
  '访客量',
921
969
  '浏览量',
@@ -945,7 +993,7 @@ class GroupBy:
945
993
  '直接成交金额': float,
946
994
  }, errors='raise')
947
995
  if is_maximize:
948
- df = df.groupby(['日期', '营销场景', '商品id', '花费', '展现量', '点击量', '人群名字'], as_index=False).agg(
996
+ df = df.groupby(['日期', '店铺名称', '营销场景', '商品id', '花费', '展现量', '点击量', '人群名字'], as_index=False).agg(
949
997
  **{'加购量': ('加购量', np.max),
950
998
  '成交笔数': ('成交笔数', np.max),
951
999
  '成交金额': ('成交金额', np.max),
@@ -954,7 +1002,7 @@ class GroupBy:
954
1002
  }
955
1003
  )
956
1004
  else:
957
- df = df.groupby(['日期', '营销场景', '商品id', '花费', '展现量', '点击量', '人群名字'], as_index=False).agg(
1005
+ df = df.groupby(['日期', '店铺名称', '营销场景', '商品id', '花费', '展现量', '点击量', '人群名字'], as_index=False).agg(
958
1006
  **{
959
1007
  '加购量': ('加购量', np.min),
960
1008
  '成交笔数': ('成交笔数', np.min),
@@ -1043,7 +1091,6 @@ class GroupBy:
1043
1091
  # df.to_csv('/Users/xigua/Downloads/test_人群分类.csv', index=False, header=True, encoding='utf-8_sig')
1044
1092
  # breakpoint()
1045
1093
  return df
1046
-
1047
1094
  elif '天猫_关键词报表' in table_name:
1048
1095
  df.rename(columns={
1049
1096
  '场景名字': '营销场景',
@@ -1065,7 +1112,7 @@ class GroupBy:
1065
1112
  '直接成交金额': float,
1066
1113
  }, errors='raise')
1067
1114
  if is_maximize:
1068
- df = df.groupby(['日期', '营销场景', '商品id', '词类型', '词名字/词包名字', '花费', '展现量', '点击量'], as_index=False).agg(
1115
+ df = df.groupby(['日期', '店铺名称', '营销场景', '商品id', '词类型', '词名字/词包名字', '花费', '展现量', '点击量'], as_index=False).agg(
1069
1116
  **{'加购量': ('加购量', np.max),
1070
1117
  '成交笔数': ('成交笔数', np.max),
1071
1118
  '成交金额': ('成交金额', np.max),
@@ -1074,7 +1121,7 @@ class GroupBy:
1074
1121
  }
1075
1122
  )
1076
1123
  else:
1077
- df = df.groupby(['日期', '营销场景', '商品id', '词类型', '词名字/词包名字', '花费', '展现量', '点击量'], as_index=False).agg(
1124
+ df = df.groupby(['日期', '店铺名称', '营销场景', '商品id', '词类型', '词名字/词包名字', '花费', '展现量', '点击量'], as_index=False).agg(
1078
1125
  **{
1079
1126
  '加购量': ('加购量', np.min),
1080
1127
  '成交笔数': ('成交笔数', np.min),
@@ -1130,7 +1177,7 @@ class GroupBy:
1130
1177
  '观看次数': int,
1131
1178
  }, errors='raise')
1132
1179
  if is_maximize:
1133
- df = df.groupby(['日期', '营销场景', '人群名字', '计划名字', '花费', '观看次数', '展现量'],
1180
+ df = df.groupby(['日期', '店铺名称', '营销场景', '人群名字', '计划名字', '花费', '观看次数', '展现量'],
1134
1181
  as_index=False).agg(
1135
1182
  **{
1136
1183
  '进店量': ('进店量', np.max),
@@ -1143,7 +1190,7 @@ class GroupBy:
1143
1190
  }
1144
1191
  )
1145
1192
  else:
1146
- df = df.groupby(['日期', '营销场景', '人群名字', '计划名字', '花费', '观看次数', '展现量'],
1193
+ df = df.groupby(['日期', '店铺名称', '营销场景', '人群名字', '计划名字', '花费', '观看次数', '展现量'],
1147
1194
  as_index=False).agg(
1148
1195
  **{
1149
1196
  '进店量': ('进店量', np.min),
@@ -1196,7 +1243,7 @@ class GroupBy:
1196
1243
  '品牌搜索人数': int,
1197
1244
  }, errors='raise')
1198
1245
  if is_maximize:
1199
- df = df.groupby(['日期', '报表类型', '花费', '展现量', '点击量'], as_index=False).agg(
1246
+ df = df.groupby(['日期', '店铺名称', '报表类型', '花费', '展现量', '点击量'], as_index=False).agg(
1200
1247
  **{
1201
1248
  '加购量': ('加购量', np.max),
1202
1249
  '成交笔数': ('成交笔数', np.max),
@@ -1206,7 +1253,7 @@ class GroupBy:
1206
1253
  }
1207
1254
  )
1208
1255
  else:
1209
- df = df.groupby(['日期', '报表类型', '花费', '展现量', '点击量'], as_index=False).agg(
1256
+ df = df.groupby(['日期', '店铺名称', '报表类型', '花费', '展现量', '点击量'], as_index=False).agg(
1210
1257
  **{
1211
1258
  '加购量': ('加购量', np.min),
1212
1259
  '成交笔数': ('成交笔数', np.min),
@@ -1263,35 +1310,7 @@ class GroupBy:
1263
1310
  }
1264
1311
  )
1265
1312
  return df
1266
- elif '店铺来源_日数据' in table_name and '旧版' not in table_name:
1267
- # 包含三级来源名称和预设索引值列
1268
- # 截取 从上月1日 至 今天的花费数据, 推广款式按此数据从高到低排序(商品图+排序)
1269
- df_visitor3 = df.groupby(['日期', '三级来源'], as_index=False).agg({'访客数': 'sum'})
1270
- df_visitor3 = df_visitor3[~df_visitor3['三级来源'].isin([''])] # 指定列中删除包含空值的行
1271
- # df_visitor = df_visitor[(df_visitor['日期'] >= f'{year_my}-{last_month.month}-01')]
1272
- df_visitor3 = df_visitor3.groupby(['三级来源'], as_index=False).agg({'访客数': 'sum'})
1273
- df_visitor3.sort_values('访客数', ascending=False, ignore_index=True, inplace=True)
1274
- df_visitor3.reset_index(inplace=True)
1275
- df_visitor3['index'] = df_visitor3['index'] + 100
1276
- df_visitor3.rename(columns={'index': '三级访客索引'}, inplace=True)
1277
- df_visitor3 = df_visitor3[['三级来源', '三级访客索引']]
1278
-
1279
- # 包含二级来源名称和预设索引值列
1280
- df_visitor2 = df.groupby(['日期', '二级来源'], as_index=False).agg({'访客数': 'sum'})
1281
- df_visitor2 = df_visitor2[~df_visitor2['二级来源'].isin([''])] # 指定列中删除包含空值的行
1282
- # df_visitor2 = df_visitor2[(df_visitor2['日期'] >= f'{year_my}-{last_month.month}-01')]
1283
- df_visitor2 = df_visitor2.groupby(['二级来源'], as_index=False).agg({'访客数': 'sum'})
1284
- df_visitor2.sort_values('访客数', ascending=False, ignore_index=True, inplace=True)
1285
- df_visitor2.reset_index(inplace=True)
1286
- df_visitor2['index'] = df_visitor2['index'] + 100
1287
- df_visitor2.rename(columns={'index': '二级访客索引'}, inplace=True)
1288
- df_visitor2 = df_visitor2[['二级来源', '二级访客索引']]
1289
-
1290
- df = pd.merge(df, df_visitor2, how='left', left_on='二级来源', right_on='二级来源')
1291
- df = pd.merge(df, df_visitor3, how='left', left_on='三级来源', right_on='三级来源')
1292
- return df
1293
- elif '天猫_店铺来源_日数据_旧版' in table_name:
1294
-
1313
+ elif '店铺流量来源构成' in table_name:
1295
1314
  # 包含三级来源名称和预设索引值列
1296
1315
  # 截取 从上月1日 至 今天的花费数据, 推广款式按此数据从高到低排序(商品图+排序)
1297
1316
  df_visitor3 = df.groupby(['日期', '三级来源'], as_index=False).agg({'访客数': 'sum'})
@@ -1373,7 +1392,7 @@ class GroupBy:
1373
1392
  )
1374
1393
  return df
1375
1394
  elif '京东_京准通' in table_name and '全站营销' not in table_name:
1376
- df = df.groupby(['日期', '产品线', '触发sku id', '跟单sku id', 'spu id', '花费', '展现数', '点击数'], as_index=False).agg(
1395
+ df = df.groupby(['日期', '店铺名称', '产品线', '触发sku id', '跟单sku id', 'spu id', '花费', '展现数', '点击数'], as_index=False).agg(
1377
1396
  **{'直接订单行': ('直接订单行', np.max),
1378
1397
  '直接订单金额': ('直接订单金额', np.max),
1379
1398
  '总订单行': ('总订单行', np.max),
@@ -1443,7 +1462,7 @@ class GroupBy:
1443
1462
  '总订单行': ('总订单行', np.max),
1444
1463
  '总订单金额': ('总订单金额', np.max),
1445
1464
  '总加购数': ('总加购数', np.max),
1446
- '下单新客数': ('下单新客数_去重', np.max),
1465
+ '下单新客数': ('下单新客数(去重)', np.max),
1447
1466
  '领券数': ('领券数', np.max),
1448
1467
  '商品关注数': ('商品关注数', np.max),
1449
1468
  '店铺关注数': ('店铺关注数', np.max)
@@ -1457,7 +1476,7 @@ class GroupBy:
1457
1476
  return df
1458
1477
  elif '天猫店铺来源_手淘搜索' in table_name:
1459
1478
  df = df.groupby(
1460
- ['日期', '关键词'],
1479
+ ['日期', '关键词', '店铺名称'],
1461
1480
  as_index=False).agg(
1462
1481
  **{
1463
1482
  '访客数': ('访客数', np.max),
@@ -1476,7 +1495,7 @@ class GroupBy:
1476
1495
  return df
1477
1496
  elif '多店推广场景_按日聚合' in table_name:
1478
1497
  df = df.groupby(
1479
- ['日期', '店铺', '营销场景'],
1498
+ ['日期', '店铺名称', '营销场景'],
1480
1499
  as_index=False).agg(
1481
1500
  **{
1482
1501
  '花费': ('花费', np.sum),
@@ -1487,7 +1506,7 @@ class GroupBy:
1487
1506
  '成交金额': ('成交金额', np.sum)
1488
1507
  }
1489
1508
  )
1490
- df.sort_values(['日期', '店铺', '花费'], ascending=[False, False, False], ignore_index=True, inplace=True)
1509
+ df.sort_values(['日期', '店铺名称', '花费'], ascending=[False, False, False], ignore_index=True, inplace=True)
1491
1510
  # df.to_csv('/Users/xigua/Downloads/test.csv', encoding='utf-8_sig', index=False, header=True)
1492
1511
  return df
1493
1512
  elif '达摩盘_人群报表' in table_name:
@@ -1497,6 +1516,7 @@ class GroupBy:
1497
1516
  print(f'<{table_name}>: Groupby 类尚未配置,数据为空')
1498
1517
  return pd.DataFrame({})
1499
1518
 
1519
+ @try_except
1500
1520
  def ret_keyword(self, keyword, as_file=False):
1501
1521
  """ 推广关键词报表,关键词分类, """
1502
1522
  datas = [
@@ -1664,6 +1684,7 @@ class GroupBy:
1664
1684
  break
1665
1685
  return result
1666
1686
 
1687
+ @try_except
1667
1688
  def set_crowd(self, keyword, as_file=False):
1668
1689
  """ 推广人群报表,人群分类, """
1669
1690
  result_a = re.findall('_a$|_a_|_ai|^a_', str(keyword), re.IGNORECASE)
@@ -1699,6 +1720,7 @@ class GroupBy:
1699
1720
  if not is_res:
1700
1721
  return ''
1701
1722
 
1723
+ @try_except
1702
1724
  def set_crowd2(self, keyword, as_file=False):
1703
1725
  """ 推广人群报表,人群分类, """
1704
1726
  datas = [
@@ -1822,7 +1844,7 @@ class GroupBy:
1822
1844
  # 推广表合并生意经 , 以推广数据为基准,销售数据不齐全
1823
1845
  df = pd.merge(df, syj, how='left', left_on=['日期', '商品id'], right_on=['日期', '宝贝id'])
1824
1846
  df.drop(labels='宝贝id', axis=1, inplace=True)
1825
- df.drop_duplicates(subset=['日期', '商品id', '花费', '销售额'], keep='last', inplace=True, ignore_index=True)
1847
+ df.drop_duplicates(subset=['日期', '店铺名称', '商品id', '花费', '销售额'], keep='last', inplace=True, ignore_index=True)
1826
1848
  df.fillna(0, inplace=True)
1827
1849
  df['成本价'] = df['成本价'].astype('float64')
1828
1850
  df['销售额'] = df['销售额'].astype('float64')
@@ -1832,6 +1854,8 @@ class GroupBy:
1832
1854
  df['毛利率'] = df.apply(lambda x: round((x['销售额'] - x['商品成本']) / x['销售额'], 4) if x['销售额'] > 0 else 0, axis=1)
1833
1855
  df['盈亏'] = df.apply(lambda x: x['商品毛利'] - x['花费'], axis=1)
1834
1856
  return df
1857
+
1858
+ @try_except
1835
1859
  def performance_concat(self, bb_tg=True):
1836
1860
  tg, zb, pxb = self.data_tgyj['天猫汇总表调用'], self.data_tgyj['天猫_超级直播'], self.data_tgyj['天猫_品销宝账户报表']
1837
1861
  zb.rename(columns={
@@ -1880,6 +1904,7 @@ class GroupBy:
1880
1904
  )
1881
1905
  return df
1882
1906
 
1907
+ @try_except
1883
1908
  def performance_jd(self, jd_tg=True):
1884
1909
  jdtg, sku_sales = self.data_jdtg['京东_京准通'], self.data_jdtg['京东_sku_商品明细']
1885
1910
  jdtg = jdtg.groupby(['日期', '跟单sku id'],
@@ -1994,7 +2019,7 @@ class GroupBy:
1994
2019
  except:
1995
2020
  print(f'{filename}: sort_values排序参数错误!')
1996
2021
  df.to_excel(os.path.join(path, filename + '.xlsx'), index=index, header=header, engine=engine, freeze_panes=freeze_panes)
1997
-
2022
+
1998
2023
 
1999
2024
  def g_group():
2000
2025
  pass
@@ -2078,16 +2103,16 @@ def data_aggregation(service_databases=[{}], months=1, is_juhe=True):
2078
2103
  },
2079
2104
  {
2080
2105
  '数据库名': '聚合数据',
2081
- '集合名': '天猫_店铺来源_日数据',
2106
+ '集合名': '店铺流量来源构成',
2082
2107
  '唯一主键': ['日期', '一级来源', '二级来源', '三级来源', '访客数'],
2083
2108
  '数据主体': sdq.dplyd(),
2084
2109
  },
2085
- {
2086
- '数据库名': '聚合数据',
2087
- '集合名': '天猫_店铺来源_日数据_旧版',
2088
- '唯一主键': ['日期', '一级来源', '二级来源', '三级来源'],
2089
- '数据主体': sdq.dplyd_old(),
2090
- },
2110
+ # {
2111
+ # '数据库名': '聚合数据',
2112
+ # '集合名': '天猫_店铺来源_日数据_旧版',
2113
+ # '唯一主键': ['日期', '一级来源', '二级来源', '三级来源'],
2114
+ # '数据主体': sdq.dplyd_old(),
2115
+ # },
2091
2116
  {
2092
2117
  '数据库名': '聚合数据',
2093
2118
  '集合名': '商品id编码表',
@@ -2102,7 +2127,7 @@ def data_aggregation(service_databases=[{}], months=1, is_juhe=True):
2102
2127
  },
2103
2128
  {
2104
2129
  '数据库名': '聚合数据',
2105
- '集合名': '商品成本',
2130
+ '集合名': '商品成本', # 暂缺 10.31
2106
2131
  '唯一主键': ['款号'],
2107
2132
  '数据主体': sdq.sp_cost(),
2108
2133
  },
@@ -2114,7 +2139,7 @@ def data_aggregation(service_databases=[{}], months=1, is_juhe=True):
2114
2139
  },
2115
2140
  {
2116
2141
  '数据库名': '聚合数据',
2117
- '集合名': '京东_京准通_全站营销',
2142
+ '集合名': '京东_京准通_全站营销', # 暂缺
2118
2143
  '唯一主键': ['日期', '产品线', '花费'],
2119
2144
  '数据主体': sdq.jdqzyx(),
2120
2145
  },
@@ -2160,18 +2185,18 @@ def data_aggregation(service_databases=[{}], months=1, is_juhe=True):
2160
2185
  '唯一主键': ['日期', '报表类型', '推广渠道', '营销场景', '花费'],
2161
2186
  '数据主体': sdq.pxb_zh(),
2162
2187
  },
2163
- {
2164
- '数据库名': '聚合数据',
2165
- '集合名': '天猫店铺来源_手淘搜索',
2166
- '唯一主键': ['日期', '关键词', '访客数'],
2167
- '数据主体': sdq.tm_search(),
2168
- },
2169
- {
2170
- '数据库名': '聚合数据',
2171
- '集合名': '生意参谋_直播场次分析',
2172
- '唯一主键': ['场次id'],
2173
- '数据主体': sdq.zb_ccfx(),
2174
- },
2188
+ # {
2189
+ # '数据库名': '聚合数据',
2190
+ # '集合名': '天猫店铺来源_手淘搜索', # 暂缺
2191
+ # '唯一主键': ['日期', '关键词', '访客数'],
2192
+ # '数据主体': sdq.tm_search(),
2193
+ # },
2194
+ # {
2195
+ # '数据库名': '聚合数据',
2196
+ # '集合名': '生意参谋_直播场次分析', # 暂缺
2197
+ # '唯一主键': ['场次id'],
2198
+ # '数据主体': sdq.zb_ccfx(),
2199
+ # },
2175
2200
  {
2176
2201
  '数据库名': '聚合数据',
2177
2202
  '集合名': '多店推广场景_按日聚合',
@@ -2198,8 +2223,8 @@ def data_aggregation(service_databases=[{}], months=1, is_juhe=True):
2198
2223
  # 由推广主体报表,写入一个商品索引表,索引规则:从上月 1 号至今花费从高到低排序
2199
2224
  m.df_to_mysql(
2200
2225
  df=g.sp_index_datas,
2201
- db_name='属性设置2',
2202
- table_name='商品索引表',
2226
+ db_name='属性设置3',
2227
+ table_name='商品索引表_主推排序调用',
2203
2228
  move_insert=False, # 先删除,再插入
2204
2229
  # df_sql=True,
2205
2230
  drop_duplicates=False,
@@ -2285,6 +2310,6 @@ def main():
2285
2310
 
2286
2311
 
2287
2312
  if __name__ == '__main__':
2288
- data_aggregation(service_databases=[{'company': 'mysql'}], months=24, is_juhe=False) # 正常的聚合所有数据
2313
+ data_aggregation(service_databases=[{'company': 'mysql'}], months=0, is_juhe=True) # 正常的聚合所有数据
2289
2314
  # data_aggregation_one(service_databases=[{'company': 'mysql'}], months=1) # 单独聚合某一个数据库,具体库进函数编辑
2290
2315
  # optimize_data.op_data(service_databases=[{'company': 'mysql'}], days=3650) # 立即启动对聚合数据的清理工作