Python 在pandas中循环查询

Python 在pandas中循环查询,python,pandas,Python,Pandas,你好,我有下面的代码 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['

你好,我有下面的代码

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')
june=q.query('20190531
我需要迭代这个查询,这样每个月都会从两边减去1,所以下一次的Itation是

june = q.query('20190431 < ndate < 20190601').copy()
ytd = q.query('20170101 < ndate < 20190431').copy()
...
njune.to_csv('path/05_19.csv')
june=q.query('20190431
然后是代码的其余部分。目标是当它达到的值时停止

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

你知道我如何循环这个吗?

不确定它是否是最好的,但我最终创建了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) :
fa=['20190531

并补充了其余的-希望有人能补充一个更好的方式

不确定这是否是最好的,但我最终创建了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) :
fa=['20190531
并补充了其余的-希望有人能补充一个更好的方式