添加Pandas Column以跨MYSQL数据表进行迭代

时间:2017-08-30 10:11:33

标签: pandas dataframe append multiple-columns

我有一个符号列表,我在其中运行查询以返回for循环中的价格数据。我希望每个查询返回一个附加到数据帧的新列。

stk_df = pd.read_sql("SELECT dt, close from futures WHERE symbol = 
'BANKNIFTY'AND dt >='" +st_dt.strftime("%Y-%m-%d")+"'AND dt <='" 
+d_t.strftime("%Y-%m-%d")+"'ORDER by dt ASC", db1)
stk_df = stk_df.set_index('dt')
query_cur.execute("SELECT a.* FROM (SELECT DISTINCT symbol FROM 
navposmod.futures a WHERE dt='2016-12-01') a, (SELECT DISTINCT symbol FROM 
navposmod.`futures` b WHERE dt='2017-08-29') b WHERE a.symbol=b.symbol AND 
a.symbol NOT IN ('NIFTYINFRA','NIFTYIT','NIFTYMID50','NIFTYPSE')")
a = query_cur.fetchall()
dummy_df = pd.DataFrame()
print len(a)
print stk_df.ix[:,0]
for x in range(1,len(a)):
    symbol_str = a[x][0]
    print "processing for " + symbol_str
    stk_df1 = pd.read_sql("SELECT close from futures WHERE symbol = 
    +d_t.strftime("%Y-%m-%d")+"'ORDER by dt ASC", db1)
    #print stk_df1.ix[:,0]
    stk_df["'"+symbol_str+"'"] = stk_df1.ix[:,0]
    print stk_df 

这会返回一个新列,但是会填充nans

0 个答案:

没有答案