#1 expand:将得到的结果拓为两列
res1[['success_days','fail_days']]=res1.apply(lambda x:(x['success_days']+1 if x['counts']==x['cnt'] else x['success_days'],
x['fail_days']+1 if x['counts']!=x['cnt'] else x['fail_days']),axis=1,result_type="expand")
#2
res1['finish_status']=res1.apply(lambda x:1 if x['success_days']+x['fail_days']==x['goal_days'] else 0,axis=1)
#结果
results=res1[['goal_id','success_days','fail_days','finish_status']]
results
其实本编主要阐述是将结果怎么批量插入数据库,防止阻塞
#需要的数据格式处理..
list_columns=",".join(results.columns)
>> list_columns
>>'goal_id,success_days,fail_days,finish_status'
res_values=list(results.apply(lambda x:tuple([i for i in x]),axis=1))
list_values=str(res_values)[1:-1]
>> list_values
>>'(5, 0, 1, 0), (33, 0, 1, 0), (34, 0, 1, 0), (35, 1, 0, 0), (37, 0, 1, 0), (42, 0, 1, 0), (44, 1, 0, 1), (46, 0, 1, 0), (47, 0, 1, 0), (48, 0, 1, 0)'
table_name="your_table_name"
#sql语句格式
res_sql=f""" insert into {table_name} ({list_columns}) values {list_values} on duplicate key update success_days=values(success_days),fail_days=values(fail_days),finish_status=values(finish_status)"""
con = conn_sql()
cursor = con.cursor() # 获取游标
cursor.execute(res_sql)
con.commit()
cursor.close()
con.close()
print('写入完成.................')