Home > Software design >  how do I merge items from a dataframe into a query?
how do I merge items from a dataframe into a query?

Time:12-29

i have a dataframe like this:

tablename columnname
t1 crd
t2 deb
t3 lon
... ...

and want to combine these 2 column into a query like this, (in Python)

sel crd from t1 union select deb from t2 union select lon from t3 ;

Thank you

CodePudding user response:

Is it what you expect?

make_query = lambda x: f"select {x['columnname']} from {x['tablename']}"
qs = ' union '.join(df.apply(make_query, axis=1))
print(qs)

# Output
'select crd from t1 union select deb from t2 union select lon from t3'
  • Related