-1
  1. 用户将 sql 查询保存在 pd 中,我想执行每个 sql 查询然后将数据框放入同一个 pd 中,如下所示
   colunm1 = 'id1' , 'id2 '  
   column2 = 'select sum(total) from table where data_mothrn ='2020-04'' , 
             'select sum(total) from table2 where data_month ='2020-04'' , 
   column3 = 'result from column2'
             'result from column2'              
  1. 我要识字这个东西?将每个 column2 结果按每一行放入 column3
list_para = []
def cacth_column2_sql():
    a = df_parameter['column2'].iloc[i]
    return a

def result_sql (p):
    df0 = pd.read_sql(p, conn)
    a = df0.iat[0, 0]
    list_para.append(a)


a = cacth_column2_sql() 
result_sql(a)

  1. 如何创建新列以自动保存结果?
4

1 回答 1

0

Your question requires more inputs from you. Please update it correctly.

Based on what I understand:

I have created a code that reads the input from user (Parameter-name, parameter-data) and shows it in the output. Each record can be accessed by Parameter-name

import pandas as pd

n_times = int(input("How many input queries you want to add"))

res = []
for i in range(0,n_times):

   paramname = input("Enter the id for the query ")
   paramdata = input("Enter the sql query ")
   #column3 = 'result from column2'
   result = pd.read_sql(paramdata, conn)
   #You can add code to check correct syntax here of input
   df1 = pd.DataFrame(data=[[paramname,paramdata,result]],columns= 
   ["Parameter-Name", "Parameter-Data","Result"])
   df1.index = df1.index + 1

print(df1)

You can access the queries by index like this

df1.iloc[0]
于 2020-05-30T04:33:19.497 回答