2016-04-20 3 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に応じて異なる場合があります)?


EDIT:追加のサンプルと期待される結果

> 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'] 
関連する問題