python - 循环遍历映射表中的参数(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循环来做呢?
解决方案
你可以试试这段代码:
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'] +"""'"""
然后您必须执行查询并获取和构建结果。
推荐阅读
- pickle - 加载 XGBoost 模型:ModuleNotFoundError:没有名为“sklearn.preprocessing._label”的模块
- python - 如何在 Django 中处理多文件输入字段
- python - Python 3.7 psycopg2 - Xcode 错误“错误:命令‘gcc’失败,退出状态为 1”
- python - 使用 Python 通过 Mailgun 提交变量时出错
- mysql - 在特定事件之前计数事件
- bash - 如何使用 alt-left 和 alt-right 在 bash 中实现“后退”和“前进”?
- r - R 空间 - 边界图
- google-maps - Google API 发票异常增加
- c - 编译成功后SDL2没有运行的问题
- python - 如果设置了其他可选值,则设置 argparse 可选值