import pandas as pd
import numpy as np
df_user_balance_table = pd.read_csv('D:\\user_balance_table.csv')
np.sort(np.unique(df_user_balance_table['report_date']))
df_user_balance_table.head()
df_tmp = df_user_balance_table.groupby(['report_date'])['total_purchase_amt','total_redeem_amt'].sum()
df_tmp.reset_index(inplace=True)
df_tmp.head()
df_tmp['year'] = df_tmp['report_date']/int(10000)
df_tmp['year'] = df_tmp['year'].astype(int)
df_tmp['month'] = ( df_tmp['report_date'] - df_tmp['year']*10000 )/100
df_tmp['month'] = df_tmp['month'].astype(int)
df_tmp['day'] = ( df_tmp['report_date'] - df_tmp['year']*10000 - df_tmp['month']*100)
df_tmp['date_str'] = df_tmp['year'].astype(str) + '-' + df_tmp['month'].astype(str) + '-' + df_tmp['day'].astype(str)
df_tmp['date_str'] = pd.to_datetime( df_tmp['date_str'])
df_tmp['weekday'] = df_tmp['date_str'].dt.weekday + 1
df_tmp[['report_date','weekday','total_purchase_amt']].head()
df_sta = pd.DataFrame(columns = ['weekday1','weekday2','weekday3','weekday4','weekday5','weekday6','weekday7'])
count = 0
for i in range(len(df_tmp)):
df_sta.loc[count,'weekday'+str(df_tmp.loc[i,'weekday'])] = df_tmp.loc[i,'total_purchase_amt']
if df_tmp.loc[i,'weekday'] == 7:
count = count + 1
df_sta['week_mean'] = df_sta.mean(axis=1)
df_sta['weekday1_coefficient'] = df_sta['weekday1']/df_sta['week_mean']
df_sta['weekday2_coefficient'] = df_sta['weekday2']/df_sta['week_mean']
df_sta['weekday3_coefficient'] = df_sta['weekday3']/df_sta['week_mean']
df_sta['weekday4_coefficient'] = df_sta['weekday4']/df_sta['week_mean']
df_sta['weekday5_coefficient'] = df_sta['weekday5']/df_sta['week_mean']
df_sta['weekday6_coefficient'] = df_sta['weekday6']/df_sta['week_mean']
df_sta['weekday7_coefficient'] = df_sta['weekday7']/df_sta['week_mean']
df_sta['weekday4_coefficient'] = df_sta['weekday4']/df_sta['week_mean']
df_sta['weekday5_coefficient'] = df_sta['weekday5']/df_sta['week_mean']
df_sta['weekday6_coefficient'] = df_sta['weekday6']/df_sta['week_mean']
df_sta['weekday7_coefficient'] = df_sta['weekday7']/df_sta['week_mean']
coefficient_list= ['weekday1_coefficient','weekday2_coefficient',
'weekday3_coefficient','weekday4_coefficient','weekday5_coefficient',
'weekday6_coefficient','weekday7_coefficient'
]
df_sta[coefficient_list].median()
df_sta.loc[60,'week_mean']* df_sta[coefficient_list].median()
df_sta_redeem = pd.DataFrame(columns = ['weekday1','weekday2','weekday3','weekday4','weekday5','weekday6','weekday7'])
count = 0
for i in range(len(df_tmp)):
df_sta_redeem.loc[count,'weekday'+str(df_tmp.loc[i,'weekday'])] = df_tmp.loc[i,'total_redeem_amt']
if df_tmp.loc[i,'weekday'] == 7:
count = count + 1
df_sta_redeem['week_mean'] = df_sta_redeem.mean(axis=1)
df_sta_redeem['weekday1_coefficient'] = df_sta_redeem['weekday1']/df_sta_redeem['week_mean']
df_sta_redeem['weekday2_coefficient'] = df_sta_redeem['weekday2']/df_sta_redeem['week_mean']
df_sta_redeem['weekday3_coefficient'] = df_sta_redeem['weekday3']/df_sta_redeem['week_mean']
df_sta_redeem['weekday4_coefficient'] = df_sta_redeem['weekday4']/df_sta_redeem['week_mean']
df_sta_redeem['weekday5_coefficient'] = df_sta_redeem['weekday5']/df_sta_redeem['week_mean']
df_sta_redeem['weekday6_coefficient'] = df_sta_redeem['weekday6']/df_sta_redeem['week_mean']
df_sta_redeem['weekday7_coefficient'] = df_sta_redeem['weekday7']/df_sta_redeem['week_mean']
df_sta_redeem[coefficient_list].median()
df_sta_redeem.loc[60,'week_mean']* df_sta_redeem[coefficient_list].median()
submit = pd.DataFrame(columns = ['report_date','purchase','redeem'])
submit['report_date'] = ['2014-09-01','2014-09-02','2014-09-03','2014-09-04','2014-09-05','2014-09-06','2014-09-07','2014-09-08','2014-09-09','2014-09-10','2014-09-11','2014-09-12','2014-09-13','2014-09-14','2014-09-15','2014-09-16','2014-09-17','2014-09-18','2014-09-19','2014-09-20','2014-09-21','2014-09-22','2014-09-23','2014-09-24','2014-09-25','2014-09-26','2014-09-27','2014-09-28','2014-09-29','2014-09-30']
submit['report_date'] = pd.to_datetime(submit['report_date'])
submit['weekday'] = submit['report_date'].dt.weekday + 1
purchasePre = df_sta.loc[60,'week_mean']* df_sta[coefficient_list].median()
submit.loc[submit['weekday']==1,'purchase'] = round(purchasePre[0],2)
submit.loc[submit['weekday']==2,'purchase'] = round(purchasePre[1],2)
submit.loc[submit['weekday']==3,'purchase'] = round(purchasePre[2],2)
submit.loc[submit['weekday']==4,'purchase'] = round(purchasePre[3],2)
submit.loc[submit['weekday']==5,'purchase'] = round(purchasePre[4],2)
submit.loc[submit['weekday']==6,'purchase'] = round(purchasePre[5],2)
submit.loc[submit['weekday']==7,'purchase'] = round(purchasePre[6],2)
redeemPre = df_sta_redeem.loc[60,'week_mean']* df_sta_redeem[coefficient_list].median()
submit.loc[submit['weekday']==1,'redeem'] = round(redeemPre[0],2)
submit.loc[submit['weekday']==2,'redeem'] = round(redeemPre[1],2)
submit.loc[submit['weekday']==3,'redeem'] = round(redeemPre[2],2)
submit.loc[submit['weekday']==4,'redeem'] = round(redeemPre[3],2)
submit.loc[submit['weekday']==5,'redeem'] = round(redeemPre[4],2)
submit.loc[submit['weekday']==6,'redeem'] = round(redeemPre[5],2)
submit.loc[submit['weekday']==7,'redeem'] = round(redeemPre[6],2)
submit[['report_date','purchase','redeem']]
submit.drop(['weekday'],axis=1,inplace=True)
submit.to_csv('D:\\tc_comp_predict_table.csv')
print (submit)
2019年04月16日 12点04分
4