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,因此下一次迭代将是
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')
知道如何循环这个吗?
解决方案
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!
推荐阅读
- r - R:如何严格使用聚合函数创建比例表?
- c++ - Win32 Listview 禁用项目标签编辑
- owl - 如何根据 owl:symmetricProperty、rdfs:subPropertyOf 和 rdfs:subClassOf 来定义 owl:equivalentClass?
- database - Postgres表大小方式大于行总和
- ethereum - Remix IDE 中的这个文本框在哪里?
- c++ - C++:在函数调用中幸存的并行随机数引擎
- javascript - React js导入HTML不会运行脚本
- git - 如何列出 git 中的分支以及 master 之前或之后的提交数?
- python-3.x - 谷歌云函数中的 Python 问题
- elasticsearch - Elasticsearch 查询返回有限数量的结果 (10),其中将包含来自每个指定关键字的 2