pandas 从SQL阅读DataFrame时仅返回某些行

baubqpgj  于 2023-03-16  发布在  其他
关注(0)|答案(1)|浏览(107)

我尝试使用SQLAlchemy将一个SQL表的一部分读入panda Dataframe ,但我似乎找不到在读取表时如何运行panda过滤器的等价物。我不想读取整个表,然后进行过滤,这似乎是浪费内存。
就拿Pandas来说,我会这么做:

list_of_codes_to_filter = ['code1', 'code2']
df = read_csv('path/to/file')
df = df[df['code'].isin(list_of_codes_to_filter)

有没有一个简单的等价物来使用panda read_sql?我正在尝试这样的东西:

from sqlalchemy import create_engine, select

engine = create_engine('sqlite:///path_to_db')
connection = engine.connect()
df = pd.read_sql(select().where('code'.isin(list_of_codes_to_filter)), connection)
ql3eal8s

ql3eal8s1#

codes_tbl = Table("codes", MetaData(), autoload_with=engine)
list_of_codes_to_filter = ["code1", "code2"]
qry = select(codes_tbl).where(codes_tbl.c.code.in_(list_of_codes_to_filter))
with engine.begin() as conn:
    df = pd.read_sql_query(qry, conn)
    """ SQL emitted:
    SELECT codes.id, codes.code 
    FROM codes 
    WHERE codes.code IN (?, ?)
    [generated in 0.00095s] ('code1', 'code2')
    """
    print(df)
    """ console output:
       id   code
    0   1  code1
    1   2  code2
    """

完成MCVE here

相关问题