'How can I extract rows from a database with python? I have a csv file with a list of 1000 ID's and I only want the rows that match these
Csv: ID, 100, 200, 300,...9900, 10000
SQL Columns:
ID Name
100 Henry
101 John
200 Eric
300 Lucy
import pandas as pd
import pyodbc
import csv
cnxn = pyodbc.connect('DRIVER={SQL Server};''SERVER=my_server;''Trusted_Connection=yes')
cursor = cnxn.cursor()
query = SELECT * FROM Table1
Solution 1:[1]
# create csv_dataframe
csv_dateframe = pd.read_csv(r"file_path")
# create sql_dataframe
sql_dateframe = pd.read_sql(query, cnxn)
# innerjoin
pd.merge(csv_dataframe,sql_dataframe,on="ID")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | S.B |
