首页 > 解决方案 > 循环遍历映射表中的参数(PYTHON、DATA FRAME)

问题描述

我有下表,我将其作为 pandas.dataframe 插入:

A B 
1 4 
1 5 
2 4 
3 7
3 8 

我想在 SQL 中创建函数,该函数将遍历“A”列的特定值,并为它们从“B”列中搜索适当的值,但由于大小不考虑 A 和 B 的所有可能组合的数据集。

下面的 SQL 脚本:

select
cust_id as customer_id,
cust_country_cd as customer_country,
income,
month,
year
from database1
where 
cust_id = '{values from column A}',
cust_country_cd = '{values from column B}'

我怎么能用for循环来做呢?

标签: pythonsqlpandas

解决方案


你可以试试这段代码:

for i = 1 To range(df.shape[0])
  strQuery = """ select
        cust_id as customer_id,
cust_country_cd as customer_country,
income,
month,
year
from database1
where 
cust_id = '"""+ df.iloc[i]['A']+"""'
AND cust_country_cd  = '"""+ df.iloc[i]['B'] +"""'"""

然后您必须执行查询并获取和构建结果。


推荐阅读