mysql - Pass object to python SQL -
I have a panda DF pulled from the ODBC connection:
Import pandas Psxl handle = pyodbc.connect ('...') as the .oq df1 = psql.frame_query ("Select from Table 1, where ... [some questions on the column]") # below is a Punds DF The above SQL query is generated from Df1 = pd.DataFrame ([[1] 'F', 11111, 500, 60], [2, 'M', 22222, 400, 30], [3, 'M', 33333 , 'Id', 'gender', 'zipcode', [54], 78], [4, 'F', 44444, 5400, 45], [5, 'M', 55555, 8914, 66]], column = [' 'Expenditure', 'age']) Now I'm the only one Want to run a different query on a separate table in the database? And as one of the criteria, remove rows that match the id with df1 (below example, which does not work). df2 = psql.frame_query ("Select 'from table 2 where id =? And in static' ('104', '106', '112', '115')", df1 [ 'Id']) # ID variable is a common unique identifier that is 2/2 tables My question is, I have to query the df2 elements As a list of how can I specify df1 ['id'] ? Like ... where in id (1,2,3, ...) , but using the list df1 ['id'] as an object will return this record where DF2 In addition to ID ID as well as other query criteria. I'm familiar w / r syntax, so my idea looks very similar to this one:
At the end of the day, I'm interested in parsing Table 1 So that contains only records in Table 2 (such as one of the required StatusCds found in Table 2). In this regard, I am certain that there is a more efficient method of calling data, and possibly in a query, but I am not adequately literate in Python or SQL.
Further comment
I have Pododing as a tag because I was originally dragging it to my SQL server using that module; Maybe pyodbc is a more effective way to use for this type of work? But I'm an R / Spreadsheet man; Panda is the easiest thing for me to learn so far.
frame_query an optional parameter A list or parameter can be used to use SQL queries in which it can be. To include a list of parameters, you can use Python's string formatting to include the correct number of placeholders. For example:
placeholder = ',' ('' '' '* * df1 [' id ']. Count ()) query = (" Choose from 2 "where id ({}) and in static ('104', '106', '112', '115')". Format (placeholder) df2 = psql.frame_query (query, parameters = df1 [' ID ']. Tolist ()) The query string will be in the table where the id (',?,?) And staticd ('104', '106', '112', '115 ') With select * if there are three IDs. There is a limit to the number of parameters that you send, so if you want to Number is too large, you can add up to several queries in batches and then the resulting Detafrem.
Comments
Post a Comment