0

I'm using pandas as pd and python 2.7

I have a bunch of queries and each query is returning serial numbers. Then I'm using that serial number in a bunch more queries. I want to append all the results to a list. When I append to an empty list, it just returns nothing. I don't know what I'm doing wrong.

 for query in list_of_queries:
     results  = pd.read_sql_query(query,connection,index_col=None)
     for serial_number in results['SerialNumbers']:
        a = []
        new_query = """ SELECT * FROM blah b where b.SerialNumber = '{}' """
        new_query = new_query.format(serial_number)
        results = pd.read_sql_query(new_query,connection,index_col = None)
        a.append(results)
1
  • Looks like this is resolved, but I wanted to point something out. Anyway you could run one query that joins the two tables instead of the loop? Commented May 4, 2017 at 2:07

1 Answer 1

1

You are resetting the list to be empty at the beginning of each for loop. This should be:

a = []
for serial_number in results['SerialNumbers']:        
    new_query = """ SELECT * FROM blah b where b.SerialNumber = '{}' """
    new_query = new_query.format(serial_number)
    results = pd.read_sql_query(new_query,connection,index_col = None)
    a.append(results)
# 'a' will now have all the results

Furthermore, it looks like you might be clobbering results because you use it as a variable name twice (once in each loop). I would suggest changing that too!

Sign up to request clarification or add additional context in comments.

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.