2

I want to map my DF values into a string:

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

I tried this ListResult = map(lambda x :SQL.format(*x),DF[['Col1','Col2']])

but the output be like

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

How can I generate a list of string completed with the values from my DF (the number of columns may vary according to the SQL)?


EDIT: add sample and expected result

> DF =  

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

expected result:

[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]
2
  • 1
    Can you add Minimal, Complete, and Verifiable example or at least sample of dataframe - 3,4 rows and desired output from sample? Commented Apr 20, 2016 at 7:47
  • Yep I can ^^ give me 5 min Commented Apr 20, 2016 at 7:50

2 Answers 2

1

I think you can add values for generating numpy array from DataFrame:

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'"]
Sign up to request clarification or add additional context in comments.

2 Comments

I was just missing the .values. I like when solution are that easy ^^ thx
Glad can help you! Good luck!
0

is that what you want?

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.