dimensions = row.get('dimensions', []) dateRangeValues = row.get('metrics', []) for header, dimension in zip(dimensionHeaders, dimensions): header = header.encode('utf-8') dimension = dimension.encode('utf-8') for i, values in enumerate(dateRangeValues): for metricHeader, value in zip(metricHeaders, values.get('values')): x = int(value) idx = gdr[0].split('-')[0]+gdr[0].split('-')[1] index = month_list.index(idx) if count == 1: all_df['users'][index] = all_df['users'][index]+ x if count == 2: all_df['newUsers'][index] = all_df['newUsers'][index] + x if count == 3: all_df['sessions'][index] = all_df['sessions'][index] + x engine = create_engine('mysql+pymysql://'+instance.get_account()+':'+instance.get_password()+'@localhost:3306/'+instance.get_db_name()) all_df.to_sql(key+'_'+'traffic_by_month',con=engine,if_exists='replace',index=False) print key+' data write into mysql success'
for i, values in enumerate(dateRangeValues): for metricHeader, value in zip(metricHeaders, values.get('values')): gjco_value_list.append(int(value)) #两个Series相加的条件是长度相等,如果不等,就给短的补充0 zerolistlen = len(all_df) - len(gjco_value_list) zerolist = [] for i in range(zerolistlen): zerolist.append(0) zero_ser = pd.Series(zerolist + gjco_value_list) #把gjcn视图的数据加到gj视图上去 if count == 1: all_df['users'] = all_df['users'] + zero_ser if count == 2: all_df['newUsers'] = all_df['newUsers'] + zero_ser if count == 3: all_df['sessions'] = all_df['sessions'] + zero_ser print all_df engine = create_engine('mysql+pymysql://' + instance.get_account() + ':' + instance.get_password() + '@localhost:3306/' + instance.get_db_name()) all_df.to_sql(key + '_' + 'traffic_byday', con=engine, if_exists='append', index=False) print key + 'data write into mysql success'