循环查询

2024-04-20 07:11:35 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我有下面的代码

june = q.query('20190531 < ndate < 20190701').copy()
ytd = q.query('20170101 < ndate < 20190531').copy()
aux = june.merge(ytd, on ='buyer-email')
june['date_diffsku'] = june['buyer-email'].map(aux[aux['target_product_x']!=aux['target_product_y']].groupby('buyer-email').ndate_y.max()).fillna(0)
samei = ytd.groupby(['buyer-email','target_product','nCustomer Paid'],as_index=False).agg({'ndate':'min'})
njune = pd.merge(june,samei[['buyer-email','target_product','ndate','nCustomer Paid']], left_on = ['buyer-email','target_product'], right_on=['buyer-email','target_product'],how ='left')
njune['ndate_y'] = njune['ndate_y'].fillna(0)
njune = njune[njune['target_product']=='a']
njune.to_csv('path/06_19.csv')

我需要迭代查询,这样每个月都会被两边的1减去,这样下一个itaration就会是

june = q.query('20190431 < ndate < 20190601').copy()
ytd = q.query('20170101 < ndate < 20190431').copy()
...
njune.to_csv('path/05_19.csv')

然后是剩下的代码。目标是当它达到

june = q.query('20170131 < ndate < 20170301').copy()
    ytd = q.query('20170101 < ndate < 20170131').copy()
    ...
    njune.to_csv('path/02_17.csv')

你知道我怎么循环这个吗?你知道吗


Tags: csvtopathtargetonemailproductbuyer
1条回答
网友
1楼 · 发布于 2024-04-20 07:11:35

我不确定这是不是最好的,但我最终创建了3个列表

fa = ['20190531 < ndate < 20190701','20190431 < ndate < 20190601']
af = ['20170101 < ndate < 20190531','20170101 < ndate < 20190431']
bf = [62019,52019]
for a,b,c in zip(fa,af,bf) :

并补充了其余的-希望有人能补充一个更好的方式!你知道吗

相关问题 更多 >