如何在python中将pandas数据框的值放入for循环中? [英] How to put values of pandas dataframe into a for loop in python?
问题描述
这是Python API Connection程序的一部分
这是DataFrame示例
This is a part of a Python API Connection program
Here is the DataFrame Sample
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程序的一部分,通常采用固定的参数值,即;区域,部门,品牌ID,开始日期,结束日期="05-04-2020",计分=总计".(结束日期和计分是固定值,其余的都在不断变化).
Here is the working function as a part of API python program which usually takes a fixed parameter values i.e; Region, Sector, Brand ID, Start Date, End Date="05-04-2020", Scoring ="total".(End Date and Scoring are fixed values, rest of them are keep changing).
#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中获取值并将其相应地放入函数参数中.
What I am trying to achieve is a for loop which takes values from the pandas DtaFrame and put it in the function parameters accordingly.
这是我尝试过的示例代码:
Here is the sample code i've tried:
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的值传递给函数
I'm quite not sure how to pass values of the DataFrame to the function dynamically
推荐答案
使用iterrows()按行迭代数据框.我相信您的数据框名称是美国.然后:
Iterate the dataframe by rows using iterrows(). I believe your dataframe name is US. Then:
for(index, row_data) in US.iterrows():
querySingleBrandTimeline('db.csv', {'region':{}.format(row_data['Region']),'sector':{}.format(row_data['Category ID']), 'brand_id':{}.format(row_data['Brand ID']), 'scoring':'total'}, 'start_date':{}.format{row_data['Start Date']},'end_date':'2020-03-31'})
这篇关于如何在python中将pandas数据框的值放入for循环中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!