我如何使用f-strings使用sqlalchemy填充数据框
我正在尝试使用F-strings使用平均名为_COUNT的数据框。我在哪里挣扎的是,我的脚本只会在列表中的最后一项中填充数据框架8112输出
var = ["8113","8114","8112"]
id_list_string = "','".join(var)
for var in id_list_string:
sql_query = f"SELECT list_id, avg(called_count) FROM list WHERE list_id IN ('{id_list_string}');"
df = pd.read_sql(sql=sql_query,con=cnx)
df
产生列表中的最后一个ID:
list_id avg(called_count)
0 8112 1.408
我想生产的是:
list_id avg(called_count)
0 8113 1.7268
1 8114 0.3802
2 8112 1.408
我缺少什么?
I am trying to populate a dataframe with the average called_count using f-strings. Where I am struggling is that my script will only populate the dataframe with last item in the list the being 8112
var = ["8113","8114","8112"]
id_list_string = "','".join(var)
for var in id_list_string:
sql_query = f"SELECT list_id, avg(called_count) FROM list WHERE list_id IN ('{id_list_string}');"
df = pd.read_sql(sql=sql_query,con=cnx)
df
the output produces the last id in the list as such:
list_id avg(called_count)
0 8112 1.408
What I am trying to produce is:
list_id avg(called_count)
0 8113 1.7268
1 8114 0.3802
2 8112 1.408
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要 for 循环,因为您将 list_id 加入元组中。
You don't need the for loop since you're joining list_id in a tuple.