1

I have a column in a DataFrame that contains the ids of records I want to return from a query.

This is what I have so far:

query = """select * from frames where recording_id in (%d) """ % (data['recording_id'].values)
mydata = pandas.read_sql(query, db_conn)

This returns a type error:

TypeError: %d format: a number is required, not numpy.ndarray

so I also tried using .tolist() instead of values but I have the same problem.

What is the proper way to pass these values?

4
  • ",".join(list(data["recording_id"])) Commented Feb 26, 2015 at 17:17
  • @JackManey I'm sorry but I'm not sure how to use your comment to help me. :( Commented Feb 26, 2015 at 20:14
  • You want a comma-separated list of the elements in your column. That's how you get it: via ",".join. Commented Feb 26, 2015 at 20:20
  • @JackManey I think I understand. I am getting a type error: TypeError: sequence item 0: expected string, numpy.int64 found but I think I can figure this out Commented Feb 26, 2015 at 20:24

1 Answer 1

1

Thanks to Jack and some googling, I managed to figure out a solution:

query = """select * from frames where recording_id in (%d) """ % (','.join([str(i) for i in data['recording_id']]))
Sign up to request clarification or add additional context in comments.

1 Comment

This is vulnerable to SQL injections as far as I understand.

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.