【发布时间】:2020-07-17 04:28:15
【问题描述】:
这是 Python API 连接程序的一部分
这是 DataFrame 示例
Region Sector Brand ID Start Date
7188 US 41 40000 2006-03-06
7189 US 41 40345 2017-11-06
7190 US 41 40123 2019-01-12
7191 US 42 40145 2001-02-06
7192 US 42 40185 2013-03-16
这是作为 API python 程序一部分的工作函数,它通常采用固定的参数值,即; Region, Sector, Brand ID, Start Date, End Date="05-04-2020", Scoring ="total"。(End Date 和 Scoring 是固定值,其余都是不断变化的)。
#This code is working if manually enter
#details of a Dataframe one by one which is inefficient.
#This code generates me a URL of a dataset using the parameters
querySingleBrandTimeline('db.csv', {'region':'us','sector':'41', 'brand_id':'40000', 'scoring':'total', 'start_date':'2006-03-06','end_date':'2020-03-31'})
我想要实现的是一个 for 循环,它从 pandas DtaFrame 中获取值并将其相应地放入函数参数中。
这是我尝试过的示例代码:
for id in US['Sector']:
for brandid in US['Brand ID']:
querySingleBrandTimeline('db.csv', {'region':{}.format(US['Region']),'sector':{}.format(US['Category ID']), 'brand_id':{}.format(US['Brand ID']), 'scoring':'total'}, 'start_date':{}.format{US['Start Date']},'end_date':'2020-03-31'})
我不太确定如何将 DataFrame 的值动态传递给函数
【问题讨论】:
-
您能提供一个示例解决方案吗?
标签: python pandas loops for-loop