2016-04-20 25 views
2

我想我的DF值映射到一個字符串大熊貓DF:使用地圖

SQL = """select * from mytable where col1={0} and col2={1}""" 

我想這ListResult = map(lambda x :SQL.format(*x),DF[['Col1','Col2']])

但輸出像

[u'select * from mytable where col1 = C and col2=o', 
u'select * from mytable where col1 = C and col2=o'] 

怎樣才能用我DF的值填充的字符串列表(列的數量可能因SQL而異)?


編輯:加樣品及預期的結果

> DF = 

- Col1 Col2 
- 0  1591354166  12387796 
- 1  1596855166  8833942 
- 2  1626196066  12584655 

預期的結果:

[select * from mytable where col1=1591354166 and col2=12387796, 
select * from mytable where col1=1596855166 and col2=8833942, 
select * from mytable where col1=1626196066 and col2=12584655] 
+1

你可以添加[最小的,完整的和可驗證的示例](http://stackoverflow.com/help/mcve)或者至少是數據幀的樣本 - 3,4行和樣本的期望輸出? – jezrael

+0

是的,我可以^^給我5分鐘 – Steven

回答

1

我認爲你可以生成numpy arrayDataFrame添加values

SQL = """select * from mytable where col1='{0}' and col2='{1}'""" 

print map(lambda x :SQL.format(*x),df[['Col1','Col2']].values) 

["select * from mytable where col1='1591354166' and col2='12387796'", 
"select * from mytable where col1='1596855166' and col2='8833942'", 
"select * from mytable where col1='1626196066' and col2='12584655'"] 
+0

我只是缺少'.values'。我喜歡當解決方案很容易^^ thx – Steven

+0

很高興可以幫助你!祝你好運! – jezrael

0

是你想要的嗎?

In [50]: ListResult = df.apply(lambda x: SQL.format(x.Col1, x.Col2), axis=1).tolist() 

In [51]: ListResult 
Out[51]: 
['select * from mytable where col1=1591354166 and col2=12387796', 
'select * from mytable where col1=1596855166 and col2=8833942', 
'select * from mytable where col1=1626196066 and col2=12584655']