如何循环查询并将结果添加到单个数据框中?
How to loop through queries and add the results to a single dataframe?
我有一个 Flask 应用程序,它当前连接到远程服务器,然后连接到特定的数据库。我有一个函数,它接受用户的日期范围输入,然后根据给定的日期查询数据。
from flask import make_response, redirect, render_template, session, url_for, Blueprint,request
import pandas as pd
import pymssql
from core.database import ConnectionFactory
from core.query import *
dat_a = Blueprint('dat_a', __name__)
ButtonPressed = 0
@dat_a.route('/download', methods=['POST','GET'])
def download():
if session.get('logged_in') != True:
return redirect(url_for('views.index'))
else:
if request.method == 'POST':
return render_template('data.html')
else:
Start = request.args.get('Start Date')
End = request.args.get('End Date')
connection = pymssql.connect(ConnectionFactory.SQL_HOST, ConnectionFactory.SQL_USER_xxx, ConnectionFactory.SQL_PW_xxx,ConnectionFactory.SQL_DB_xxx)
cur=connection.cursor()
query_1 = """
select some column from table
from xx
where date BETWEEN (%s) AND (%s)
"""
query_2 = """
select some column from table
"""
results = []
q_list = [query_1,query_2]
for query in q_list:
cur.execute(query,(Start,End))
results.append(cur)
print(results)
columns = [d[0] for d in cur.description]
data = pd.DataFrame(cur.fetchall(),columns=columns)
print(data)
resp = make_response(data.to_csv())
resp.headers["Content-Disposition"] = "attachment; filename=Data.csv"
resp.headers["Content-Type"] = "text/csv"
return resp
我遇到的问题是让我的 for 循环将 query_1 中的数据存储到列表中,然后将其移动到我的数据框中。目前来自 query_2 的数据将存储到列表中,然后推送到数据框,但这是不准确的,因为它只是在那时获取随机数据。
results = []
q_list = [query_1,query_2]
for query in q_list:
cur.execute(query,(Start,End))
results.append(cur)
print(results)
columns = [d[0] for d in cur.description]
data = pd.DataFrame(cur.fetchall(),columns=columns)
我尝试了一个嵌套的 for 循环,它分别调用每个查询,有自己的连接和数据框,但这也没有改变结果。 pandas 是否有更有效的方法?
您可能正在寻找这样的东西...
cur = connection.cursor()
results = []
for query, args in [
("select some_column from xx where date BETWEEN (%s) AND (%s)", (Start, End)),
("select some_column from yy", ()),
]:
cur.execute(query, args)
results.extend(cur.fetchall())
# Will be overwritten on each iteration, but it's fine as long as the columns are the same for each query
columns = [d[0] for d in cur.description]
data = pd.DataFrame(results, columns=columns)
我有一个 Flask 应用程序,它当前连接到远程服务器,然后连接到特定的数据库。我有一个函数,它接受用户的日期范围输入,然后根据给定的日期查询数据。
from flask import make_response, redirect, render_template, session, url_for, Blueprint,request
import pandas as pd
import pymssql
from core.database import ConnectionFactory
from core.query import *
dat_a = Blueprint('dat_a', __name__)
ButtonPressed = 0
@dat_a.route('/download', methods=['POST','GET'])
def download():
if session.get('logged_in') != True:
return redirect(url_for('views.index'))
else:
if request.method == 'POST':
return render_template('data.html')
else:
Start = request.args.get('Start Date')
End = request.args.get('End Date')
connection = pymssql.connect(ConnectionFactory.SQL_HOST, ConnectionFactory.SQL_USER_xxx, ConnectionFactory.SQL_PW_xxx,ConnectionFactory.SQL_DB_xxx)
cur=connection.cursor()
query_1 = """
select some column from table
from xx
where date BETWEEN (%s) AND (%s)
"""
query_2 = """
select some column from table
"""
results = []
q_list = [query_1,query_2]
for query in q_list:
cur.execute(query,(Start,End))
results.append(cur)
print(results)
columns = [d[0] for d in cur.description]
data = pd.DataFrame(cur.fetchall(),columns=columns)
print(data)
resp = make_response(data.to_csv())
resp.headers["Content-Disposition"] = "attachment; filename=Data.csv"
resp.headers["Content-Type"] = "text/csv"
return resp
我遇到的问题是让我的 for 循环将 query_1 中的数据存储到列表中,然后将其移动到我的数据框中。目前来自 query_2 的数据将存储到列表中,然后推送到数据框,但这是不准确的,因为它只是在那时获取随机数据。
results = []
q_list = [query_1,query_2]
for query in q_list:
cur.execute(query,(Start,End))
results.append(cur)
print(results)
columns = [d[0] for d in cur.description]
data = pd.DataFrame(cur.fetchall(),columns=columns)
我尝试了一个嵌套的 for 循环,它分别调用每个查询,有自己的连接和数据框,但这也没有改变结果。 pandas 是否有更有效的方法?
您可能正在寻找这样的东西...
cur = connection.cursor()
results = []
for query, args in [
("select some_column from xx where date BETWEEN (%s) AND (%s)", (Start, End)),
("select some_column from yy", ()),
]:
cur.execute(query, args)
results.extend(cur.fetchall())
# Will be overwritten on each iteration, but it's fine as long as the columns are the same for each query
columns = [d[0] for d in cur.description]
data = pd.DataFrame(results, columns=columns)