Build SQL query string using IN with a python list

前端 未结 1 2000
无人及你
无人及你 2021-01-27 03:24

I built a list of values of interest in pandas.

table1 = pd.read_csv(\"logswithIPs.csv\")
cips = data_dash[\'ip\'].unique().tolist()
print(cips[:10])
[\'111.111.         


        
相关标签:
1条回答
  • 2021-01-27 03:36

    I would export/save data_dash['ip'].unique() as an SQL table, so that it could be efficiently used for subqueries:

    pd.DataFrame({'ip':data_dash['ip'].unique()}).to_sql('tmp_ip', conn, if_exists='replace')
    

    now you can use it on the SQL DB side:

    qry = """
    select count(*) as count, url
    from tab_name
    where c_ip in (select ip from tmp_ip)
    group by url
    """
    
    filterIPs = pd.read_sql(qry, conn)
    
    0 讨论(0)
提交回复
热议问题