0

I'm using pandas and pandasql with this sample. I am trying to figure out the best way to apply 2 different conditions until one is not empty, or to skip when both results are empty.

So far I've tried comparisons within the SQL.

p = row['main']\
query = "SELECT df2.main From df2 WHERE df2.main =
\'%s\' OR df2.main =
"sub1" || \'%s\' OR df2.main = 'Sub2' ||
\'%s\' and Region = \"Example\"" % p

I have a feeling doing this through the SQL would be more efficient. It doesn't seem to work like it needs too.

Then I tried using "Try", but it keeps getting stuck on substitution 1 .

query = "SELECT df2.main From df2 WHERE df2.Number = \'%s\' and Region = \"Example\"" % p

q in this case is the query/dataframe

if q.empty:
      try:
        p = row['Substitution_1']
        print('sub1')
        # Getting stuck on this
      except q.empty:
        p = row['Substitution_2']
        print("sub2")

Would anyone have any hints or suggestions on ways to approach this better?

khelwood
  • 52,115
  • 13
  • 74
  • 94
Robit010
  • 51
  • 1
  • 7
  • I suggest you look at [How to use variables in SQL statement in Python?](https://stackoverflow.com/q/902408/3890632). Gluing values directly into your SQL is rarely appropriate. – khelwood Mar 08 '22 at 15:35

0 Answers0