首页 > 解决方案 > 在熊猫中循环查询

问题描述

你好我有下面的代码

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,因此下一次迭代将是

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')

知道如何循环这个吗?

标签: pythonpandas

解决方案


Not sure if it's the best out there but I ended up creating 3 lists

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) :

and added the rest - Hope that someone can add a better way!


推荐阅读